后台启动 Activity

Android Q 之后,都不能使用 startActivity 在后台启动了。

详情见:https://developer.android.google.cn/guide/components/activities/background-starts?hl=zh-cn

可以使用fullscreen intent 达到同样的效果。

public class NotificationUtils extends ContextWrapper {
    public static final String TAG = NotificationUtils.class.getSimpleName();

    public static final String id = "channel_1";
    public static final String name = "notification";
    private NotificationManager manager;
    private Context mContext;

    public NotificationUtils(Context base) {
        super(base);
        mContext = base;
    }

    @RequiresApi(api = 26)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }

    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }

    public void sendNotificationFullScreen( String title, String content, String type) {
        if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel();
            Notification notification = getChannelNotificationQ
                    (title, content, type);
            getManager().notify(1, notification);
        }
    }

    public void clearAllNotifiication(){
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
    }

    public  Notification getChannelNotificationQ(String title, String content, String type) {
        Intent fullScreenIntent = new Intent(this, MainActivity.class);
        fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        fullScreenIntent.putExtra("action", "callfromdevice");
        fullScreenIntent.putExtra("type", type);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, id)
                        .setSmallIcon(R.mipmap.ic_logo)
                        .setContentTitle(title)
                        .setTicker(content)
                        .setContentText(content)
                        .setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setCategory(Notification.CATEGORY_CALL)
                        .setFullScreenIntent(fullScreenPendingIntent,true);

        Notification incomingCallNotification = notificationBuilder.build();
        return incomingCallNotification;
    }

}

在后台service中调用:

 NotificationUtils notificationUtils = new NotificationUtils(MyService.this);
 String content = "fullscreen intent test";
 notificationUtils.sendNotificationFullScreen(getString(R.string.app_name), content, type);

测试的时候发现如果系统通知栏还有应用的通知消息,就只会收到通知,不会弹出页面。需要再弹出页面的时候清空一下之前的通知。

调用以下代码:

NotificationUtils notificationUtils = new NotificationUtils(this);
notificationUtils.clearAllNotifiication();

同时使用singleinstance模式进行启动

以Android 10或更高版本为目标并使用具有全屏意图的通知的应用必须在其应用的清单文件中请求USE_FULL_SCREEN_INTENT权限.

你可能感兴趣的:(android笔记,android)