当我的应用程序在 Android 10/Q 关闭时如何触发启动 Activity 意图?

How to trigger a launch Activity intent when my app is closed on Android 10/Q?

我正在尝试创建一个将在指定时间打开另一个应用程序的应用程序。为此,我使用了一个启动服务的 AlarmManager。如果在触发警报时打开我的应用程序,它就可以正常工作。我收到服务启动的通知,另一个应用程序打开。但是,如果我的应用程序在后台(按下主页按钮后),并且警报触发,我会收到服务已启动的通知,但其他应用程序不会启动。我究竟做错了什么?我正在 Pixel 3 模拟器上对此进行测试 运行 API 级别 29 (Android 10/Q).

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public static final int REQUEST_CODE=101;
    public static int aHour;
    public static int aMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void setAlarm() {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(this, amReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, aHour);
        calendar.set(Calendar.MINUTE, aMinute);
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    //Some code that sets aHour and aMinute

    //Some code that triggers setAlarm()

}

amReciever.java

public class amReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, launcherService.class);
        ContextCompat.startForegroundService(getApplicationContext(), i);
    }
}

launcherService.java

public class launcherService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("App is launching.")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);

        Intent launcher = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.example.app");
        if (launcher != null) {
            startActivity(launcher);
        }
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

AndroidManifest.xml

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

       <service android:name=".launcherService"
            android:enabled="true"
            android:exported="true" />

截至 Android 10(API 级别 29),you cannot start activities from the background anymore

此规则有许多 exceptions 可能适用于您给定的场景,也可能不适用。

如果有 none 个例外情况适用,您可能需要考虑显示 high-priority notification, possibly with a full-screen Intent