简体   繁体   English

从后台服务启动Android Activity

[英]Starting an Android Activity from a background Service

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. 我已经建立了最好的方法是创建一个自定义的broadcastReceiver,它查找Intent.SCREEN_OFF intent并将其覆盖。

Inside this custom broadcastReceiver, I'm starting the Activity. 在此自定义的broadcastReceiver中,我正在启动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 { / }; android.content.ActivityNotFoundException:无法找到显式的活动类{ / }; have you declared this activity in your AndroidManifest.xml? 您是否在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. 它可以从另一个Activity正常工作。 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." 正如Todd所建议的那样,“错误使它听起来好像以某种方式失去了对应该调用Activity的跟踪。” How is this possible? 这怎么可能? Is it a bug? 是虫子吗?

So, here's the broadcastReceiver code: 因此,这是broadcastReceiver代码:

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. 我什至可以从此Service调用它,但只能在它收到该错误之前调用它一次。

Help will be greatly appreciated! 帮助将不胜感激!

Try changing your intent and, perhaps, the activity's intent filter to call the activity using the filter rather than KioskPlayer.class. 尝试更改您的意图,并可能更改活动的意图过滤器,以使用过滤器而不是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; 不记得详细信息,但是可以确定它与Intent的范围有关:类方法是应用程序的“内部”; the filter allows you to call stuff outside your app. 过滤器使您可以在应用程序外部调用内容。 And broadcast receivers may need the latter. 广播接收者可能需要后者。

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." 这无疑回答了托德的问题:“错误使我们听起来似乎迷失了应该调用Activity的位置。” I had disabled it programatically, so it could no longer be found by the system. 我已经以编程方式禁用了它,因此系统不再能够找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM