Reputation: 1
I have been searching out a lot of things to sove my problem but none of the solutions I found worked in my case :'(
Here is what I am trying to do:
- When the screen is off, my BroadCastReceiver detects it.
- Once screen_off is detected, acquire WakeLock and my BroadCastReceiver starts my custom idle screen activity.
(As for the location where it starts the idle screen activity, I have tried in BroadCastReceiver, IntentService and AsyncTask classes but all of them made same problem)
And this is the error message I am getting:
01-25 14:55:13.253: E/ActivityThread(10879): Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BCReceiver@41fb1e48 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-25 14:55:13.253: E/ActivityThread(10879): android.app.IntentReceiverLeaked: Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BCReceiver@41fb1e48 that was originally registered here. Are you missing a call to unregisterReceiver()?
Here's my code:
MainActivity.java
public class MainActivity extends Activity {
BCReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//register receiver
mReceiver = new BCReceiver();
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
BCReceiver.java
public class BCReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.PARTIAL_WAKE_LOCK, "com.foreseeson.visionsaylauncher");
wl.acquire(); //works fine until here
Intent startHomescreen=new Intent(context, IdleScreenActivity.class);
startHomescreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(startHomescreen);
}
}
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="IdleScreenActivity"></activity>
</application>
</manifest>
Everything until "WakeLock" is working but starting an activity from the BroadCastReceiver makes the error. Some people said to put "unregisterReceiver(...)" in "onStop()" but this does not work to me because screen_off can never be detected as my Receiver gets unregistered before the screen_off event happens. Any other thoughts? Please help!
Edit: I am creating a kiosk application now. So if there is nobody playing with my kiosk device and some amount of time passes, the screen goes off and it should display my activity on the screen. I have searched that the best way to detect user-inactivity is to detect screen_off. Therefore, once screen_off is detected, it should wake up itself and start an activity.
Upvotes: 0
Views: 272
Reputation: 3468
Going off your requirements,
1. When the screen is off, my BroadCastReceiver detects it.
2. Once screen_off is detected, acquire WakeLock and my BroadCastReceiver starts my custom idle screen activity.
Ok, the screen is off, you should not be waking the phone up to display anything at this point. The screen went off because the user isn't using the app.
If you want to display an idle screen for the user to see upon returning to the app, all you need to do is display this screen in onResume(). If you want persistent monitoring use a service and notifications to alert users about events. If you app needs to stay on, you should be acquiring the wake lock before the app goes to the background and releasing it when it no longer is necessary.
Upvotes: 0