关闭我的应用程序后如何触发启动意图?

问题描述 投票:0回答:1

我正在尝试创建一个将在指定时间打开另一个应用的应用。为此,我使用了启动服务的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个回答
0
投票

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

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

如果没有例外,您可能要考虑显示high-priority notification,可能带有full-screen Intent


0
投票

@ Ryan Mentley正确指出,这将在Android 10中引起问题。但是,如果您想以任何方式实现它,请尝试以下操作:launcherService

public class launcherService extends Service {
    // Other stuff...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Other stuff...
        // Instead of this:
        Intent launcher = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.example.app");
        // Try this:
        Intent launcher = new Intent(getBaseContext(), MainActivity.class);
        launcher.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (launcher != null) {
            startActivity(launcher);
        }
        // Other stuff...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.