码迷,mamicode.com
首页 > 移动开发 > 详细

Android Q 后台启动 Activity

时间:2020-03-16 15:04:43      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:his   方法   context   nbsp   simple   通知   one   oid   str   

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

标签:his   方法   context   nbsp   simple   通知   one   oid   str   

原文地址:https://www.cnblogs.com/george-cw/p/12503566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!