質問

I have a requirement to start an activity when the screen is idle after a certain time. I have established the best way is to create a custom broadcastReceiver which looks for the Intent.SCREEN_OFF intent and override it.

Inside this custom broadcastReceiver, I'm starting the Activity. It works the first time the screen goes off, but it doesn't work again until the app is uninstalled and reinstalled.

I'm getting the following error:

android.content.ActivityNotFoundException: Unable to find explicit activity class {/}; have you declared this activity in your AndroidManifest.xml?

To answer this rather obvious question, yes. I have.

<activity
        android:name="gold.KioskPlayer"
        android:configChanges="orientation|keyboardHidden"
        android:enabled="true"
         android:launchMode="singleInstance"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

It works from another Activity fine. Furthermore, if the Manifest was wrong, then it wouldn't work full stop.

As Todd has suggested, "The error makes it sound like it somehow loses track of what Activity is supposed to be called." How is this possible? Is it a bug?

So, here's the broadcastReceiver code:

BroadcastReceiver screenoff = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

        Log.d("ScreenSaver", "ScreenSaver1 - ScreenOff");
        //Log.d("Power Lock Pressed",   "Power Button Off Pressed:" + intent.getAction());
        if(!setup.screensaverShown || !setup.canSleep)
        {
            releaseWakeLock();
            try {
                setup.wl = setup.pm.newWakeLock(
                           PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                           "ScreenSaver");
                setup.wl.acquire();
                Thread.sleep(100);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try{
                KioskPlayer.setup.screensaver=true;
                Log.d("ScreenSaver", "ScreenSaver1 starting...");
                Intent i = new Intent(getBaseContext(), KioskPlayer.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplication().startActivity(i);
            }catch(Exception e)
            {
                Log.d("ScreenSaver", "ScreenSaver1 " + e.toString());
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
};


BroadcastReceiver screenon = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(setup.canSleep)
        {
            setup.canSleep=false;
        }

    }

};


public void releaseWakeLock(){
    try {
        if ( setup.wl != null && setup.wl.isHeld() )
        {
            setup.wl.release();
            setup.wl = null;
        }
    }catch(Exception e){}
}

I can call the Activity from another Activity fine. I can even call it from this Service fine, but once and once only before it gets that error.

Help will be greatly appreciated!

役に立ちましたか?

解決 2

Turns out it was because I was disabling the class component, which meant it could only be launched by the app itself.

This certainly answers Todd's question: "The error makes it sound like it somehow loses track of what Activity is supposed to be called." I had disabled it programatically, so it could no longer be found by the system.

他のヒント

Try changing your intent and, perhaps, the activity's intent filter to call the activity using the filter rather than KioskPlayer.class. I had a similar problem a while ago; don't remember the details, but pretty sure it was about the scope of Intent: the class approach is "internal" to your app; the filter allows you to call stuff outside your app. And broadcast receivers may need the latter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top