开发者

Activity will not start after ACTION_SCREEN_ON is received

开发者 https://www.devze.com 2022-12-15 03:04 出处:网络
I am creating a Lock Replacement application which obviously requires it to have an activity that starts the ACTION_SCREEN_ON is called. These are the portions of my code relevant to it:

I am creating a Lock Replacement application which obviously requires it to have an activity that starts the ACTION_SCREEN_ON is called. These are the portions of my code relevant to it:

public class StartupBroadcastReceiver extends BroadcastReceiver {
@Override
开发者_如何学JAVApublic void onReceive(Context context, Intent intent) {
    Intent startupIntent = new Intent(context, Lockdown.class); // substitute with your launcher class
    startupIntent.addCategory(Intent.CATEGORY_HOME);
    startupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(startupIntent);
}

}

Since ACTION_SCREEN_ON cannot be called from the Manifest I registered it dynamically in my main activity.

This is in my onCreate function of my main class (Lockdown)

IntentFilter filter = new IntentFilter (Intent.ACTION_SCREEN_ON);

BroadcastReceiver mReceiver = new StartupBroadcastReceiver();
    registerReceiver(mReceiver, filter);

Any help is highly appreciated.


I found the answer to this problem:

Adding android:launchMode="singleInstance" and <category android:name="android.intent.category.DEFAULT" /> to the manifest of your main activity. You also have to add this flag to the receiver startupIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

0

精彩评论

暂无评论...
验证码 换一张
取 消