Android Q 后台启动 Activity

Posted 这么近_那么远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Q 后台启动 Activity相关的知识,希望对你有一定的参考价值。

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

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

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

 

 1 public class NotificationUtils extends ContextWrapper {
 2     public static final String TAG = NotificationUtils.class.getSimpleName();
 3 
 4     public static final String id = "channel_1";
 5     public static final String name = "notification";
 6     private NotificationManager manager;
 7     private Context mContext;
 8 
 9     public NotificationUtils(Context base) {
10         super(base);
11         mContext = base;
12     }
13 
14     @RequiresApi(api = 26)
15     public void createNotificationChannel() {
16         NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
17         getManager().createNotificationChannel(channel);
18     }
19 
20     private NotificationManager getManager() {
21         if (manager == null) {
22             manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
23         }
24         return manager;
25     }
26 
27     public void sendNotificationFullScreen( String title, String content, String type) {
28         if (Build.VERSION.SDK_INT >= 26) {
29             createNotificationChannel();
30             Notification notification = getChannelNotificationQ
31                     (title, content, type);
32             getManager().notify(1, notification);
33         }
34     }
35 
36     public void clearAllNotifiication(){
37         NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
38         notificationManager.cancelAll();
39     }
40 
41     public  Notification getChannelNotificationQ(String title, String content, String type) {
42         Intent fullScreenIntent = new Intent(this, MainActivity.class);
43         fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
44         fullScreenIntent.putExtra("action", "callfromdevice");
45         fullScreenIntent.putExtra("type", type);
46         PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
47 
48         NotificationCompat.Builder notificationBuilder =
49                 new NotificationCompat.Builder(this, id)
50                         .setSmallIcon(R.mipmap.ic_logo)
51                         .setContentTitle(title)
52                         .setTicker(content)
53                         .setContentText(content)
54                         .setAutoCancel(true)
55                         .setDefaults(Notification.DEFAULT_ALL)
56                         .setPriority(NotificationCompat.PRIORITY_MAX)
57                         .setCategory(Notification.CATEGORY_CALL)
58                         .setFullScreenIntent(fullScreenPendingIntent,true);
59 
60         Notification incomingCallNotification = notificationBuilder.build();
61         return incomingCallNotification;
62     }
63 
64 }

 

在后台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();

 

这样每次都可以在Android Q中弹出页面了。

噢。耶。

仅测试一台红米Android Q,不知此方法是否使用所有Android Q 手机

以上是关于Android Q 后台启动 Activity的主要内容,如果未能解决你的问题,请参考以下文章

实战|Android后台启动Activity实践之路续

如何在android Q os中从后台服务启动活动,而不从开发人员选项的设置中单击“允许后台活动启动”?

在后台运行的 Android 应用不需要 Activity

关于android编程中service和activity的区别

从锁定屏幕启动时,Android Activity 会打开两次

Android添加全屏启动画面