• Android app保活(前台服务)


    国内厂商定制,除非厂商给app白名单,否则只能用户手动添加白名单(应用自启和后台运行),才能通过前台服务实现app保活。

    这里介绍前台服务相关实现方式。

    开启服务:

    1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    2. //安卓8.0以上开启为前台服务
    3. startForegroundService(new Intent(this, KeepAliveNotificationService.class));
    4. } else {
    5. startService(new Intent(this, KeepAliveNotificationService.class));
    6. }

    服务:

    1. public class KeepAliveNotificationService extends Service {
    2. private final String CHANNEL_ONE_ID = "100";
    3. @Nullable
    4. @Override
    5. public IBinder onBind(Intent intent) {
    6. return null;
    7. }
    8. @Override
    9. public void onCreate() {
    10. super.onCreate();
    11. //创建通知栏常驻通知
    12. initNotification();
    13. }
    14. @Override
    15. public int onStartCommand(Intent intent, int flags, int startId) {
    16. // return super.onStartCommand(intent, flags, startId);
    17. //返回START_STICKY,被系统或手动清理后可重启
    18. return START_STICKY;
    19. }
    20. @Override
    21. public void onDestroy() {
    22. stopForeground(true);
    23. }
    24. /**
    25. * 开启通知栏
    26. */
    27. private void initNotification() {
    28. //获取管理器
    29. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    30. //创建点击跳转Activity
    31. Intent intent = new Intent(this, NotificationActivity.class);
    32. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    33. //创建notification
    34. NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ONE_ID)
    35. .setContentIntent(pendingIntent) // 设置PendingIntent
    36. .setSmallIcon(R.mipmap.user_notification_ic_launcher) // 设置状态栏内的小图标
    37. //.setLargeIcon(bitmapIcon)// 设置大图标
    38. .setContentTitle("推送服务")
    39. .setContentText("应用更好的接收推送服务") // 设置内容
    40. .setWhen(System.currentTimeMillis())// 设置该通知发生的时间
    41. .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)// 锁屏显示全部通知
    42. //.setDefaults(Notification.DEFAULT_ALL)// //使用默认的声音、振动、闪光
    43. .setCategory(Notification.CATEGORY_SERVICE)//设置类别
    44. .setPriority(NotificationCompat.PRIORITY_MAX);// 优先级为:重要通知
    45. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    46. //安卓8.0以上系统要求通知设置Channel,否则会报错
    47. NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, "服务常驻通知", NotificationManager.IMPORTANCE_HIGH);
    48. notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//锁屏显示全部通知
    49. manager.createNotificationChannel(notificationChannel);
    50. builder.setChannelId(CHANNEL_ONE_ID);
    51. }
    52. Notification notification = builder.build(); // 获取构建好的Notification
    53. //notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
    54. notification.flags = Notification.FLAG_NO_CLEAR;//不消失的常驻通知
    55. startForeground(1, notification);//设置常驻通知
    56. }
    57. }

    清单文件

    1. <service
    2. android:name="com.xx.xxxx.service.KeepAliveNotificationService"
    3. android:directBootAware="true"
    4. android:enabled="true"
    5. android:exported="true"
    6. android:foregroundServiceType="phoneCall|mediaPlayback|dataSync|mediaProjection|connectedDevice|location"
    7. android:label="@string/app_name" />

  • 相关阅读:
    openssl中SM2、SM3、SM4使用实例
    [算法]数组给定长度的所有排列
    2. zk集群部署
    heic图片如何转为jpg格式
    FFmpeg入门详解之110:RTSP协议讲解
    Junit4 一直处于运行中的排查过程
    CSP-J/S 2023第一轮认证晋级分数线有些爆冷,超出想象
    internship:接口案例实现
    简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
    第 5 章理解 ScrollView 并构建 Carousel UI
  • 原文地址:https://blog.csdn.net/qq_21467035/article/details/132689125