9tontruck 0 Newbie Poster

Hi, 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:
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.
(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();
                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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.