【问题标题】:How to trigger a launch Activity intent when my app is closed on Android 10/Q?当我的应用在 Android 10/Q 上关闭时,如何触发启动 Activity 意图?
【发布时间】:2020-07-01 04:20:09
【问题描述】:

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

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" />

【问题讨论】:

    标签: java android android-studio android-intent android-service


    【解决方案1】:

    从 Android 10(API 级别 29)开始,you cannot start activities from the background anymore。

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

    如果不适用任何例外情况,您可能需要考虑显示high-priority notification,可能还带有full-screen Intent。

    【讨论】:

    • 感谢您的帮助!我刚开始开发 Android,所以这对我来说意义重大。
    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2015-10-07
    • 1970-01-01
    • 2016-07-06
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多