zoukankan      html  css  js  c++  java
  • 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 手机

  • 相关阅读:
    完美主义之我见
    职场-位置思维
    我的读书观
    人力资源是组织的第一战略资源-论基层员工
    积累是做成事情得唯一途径
    地理信息数据处理之我见
    word 之 插入删除空行
    OSMeteorTranslationAPI(百度,有道)对比
    CsharpOSMeteorCodeGenerator(Metero代码生成器)
    HtmlDOM 文档读取研究
  • 原文地址:https://www.cnblogs.com/george-cw/p/12503566.html
Copyright © 2011-2022 走看看