我想在解锁屏幕时直接打开我的应用程序,我使用 ACTION_USER_PRESENT 但不使用我的代码,
ScreenReciver 文件
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"||||||||||||||||||");
System.out.println("Received broadcast: " + intent.getAction());
System.out.println(":::::::::::::::::::::");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)||intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent launchIntent = new Intent(context, MainActivity.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launchIntent);
}
}
manifest.xml
<receiver android:name=".ScreenReceiver"
android:exported="true" android:directBootAware="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_UNLOCKED" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
在现代版本的 Android 上,您无法从后台启动活动,除非在有限的情况下。