Note that there are some explanatory texts on larger screens.

plurals
  1. POStarting an Android Activity from a background Service
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>I'm getting the following error:</p> <blockquote> <p>android.content.ActivityNotFoundException: Unable to find explicit activity class {<em>/</em>}; have you declared this activity in your AndroidManifest.xml?</p> </blockquote> <p>To answer this rather obvious question, yes. I have.</p> <pre><code>&lt;activity android:name="gold.KioskPlayer" android:configChanges="orientation|keyboardHidden" android:enabled="true" android:launchMode="singleInstance" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.HOME" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; </code></pre> <p>It works from another Activity fine. Furthermore, if the Manifest was wrong, then it wouldn't work full stop.</p> <p>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?</p> <p>So, here's the broadcastReceiver code:</p> <pre><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 &amp;&amp; setup.wl.isHeld() ) { setup.wl.release(); setup.wl = null; } }catch(Exception e){} } </code></pre> <p>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.</p> <p>Help will be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload