当我的应用在Android

当我的应用在Android

本文介绍了当我的应用在Android 10/Q上关闭时,如何触发启动活动意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an app that will open another app at a specified time. To do this, I used an AlarmManager that starts a service. It works just fine if my app is open when the alarm is triggered. I get a notification that the service started, and the other app opens. However, if my app is in the background (after pressing the home button), and the alarm triggers, I get a notification that the service started, but the other app does not launch. What am I doing wrong? I am testing this on a Pixel 3 emulator running API level 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)开始,您不能再从后台开始活动.

As of Android 10 (API level 29), you cannot start activities from the background anymore.

此规则有许多例外可能适用于您给定的情况,也可能不适用.

There are a number of exceptions to this rule that may or may not apply to your given scenario.

如果所有例外均不适用,则您可能需要考虑显示高优先级通知,可能带有全屏Intent .

If none of the exceptions apply, you might want to consider displaying a high-priority notification, possibly with a full-screen Intent.

这篇关于当我的应用在Android 10/Q上关闭时,如何触发启动活动意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:38