Android app保活(前台服务)

这篇具有很好参考价值的文章主要介绍了Android app保活(前台服务)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

开启服务:

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

服务:

public class KeepAliveNotificationService extends Service {
    private final String CHANNEL_ONE_ID = "100";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        //创建通知栏常驻通知
        initNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //        return super.onStartCommand(intent, flags, startId);
        //返回START_STICKY,被系统或手动清理后可重启
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
    }

    /**
     * 开启通知栏
     */
    private void initNotification() {
        //获取管理器
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //创建点击跳转Activity
        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        //创建notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ONE_ID)
                .setContentIntent(pendingIntent) // 设置PendingIntent
                .setSmallIcon(R.mipmap.user_notification_ic_launcher) // 设置状态栏内的小图标
                //.setLargeIcon(bitmapIcon)// 设置大图标
                .setContentTitle("推送服务")
                .setContentText("应用更好的接收推送服务") // 设置内容
                .setWhen(System.currentTimeMillis())// 设置该通知发生的时间
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)// 锁屏显示全部通知
                //.setDefaults(Notification.DEFAULT_ALL)// //使用默认的声音、振动、闪光
                .setCategory(Notification.CATEGORY_SERVICE)//设置类别
                .setPriority(NotificationCompat.PRIORITY_MAX);// 优先级为:重要通知


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //安卓8.0以上系统要求通知设置Channel,否则会报错
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, "服务常驻通知", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//锁屏显示全部通知
            manager.createNotificationChannel(notificationChannel);
            builder.setChannelId(CHANNEL_ONE_ID);
        }
        Notification notification = builder.build(); // 获取构建好的Notification
        //notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        notification.flags = Notification.FLAG_NO_CLEAR;//不消失的常驻通知
        startForeground(1, notification);//设置常驻通知

    }
}

清单文件文章来源地址https://www.toymoban.com/news/detail-702314.html

  <!-- 自定义 前台服务 -->
        <service
            android:name="com.xx.xxxx.service.KeepAliveNotificationService"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="phoneCall|mediaPlayback|dataSync|mediaProjection|connectedDevice|location"
            android:label="@string/app_name" />

到了这里,关于Android app保活(前台服务)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android 前台服务

    1.服务是什么(Service) Service 是一种可在后台执行长时间运行操作而不提供界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。此外,组件可通过绑定到服务与之进行交互,甚至是执行进程间通信 (IPC)。例如,服务可在后台

    2024年02月16日
    浏览(34)
  • Android的前台服务

    概述 前台服务是用户主动意识到的一种服务,因此在内存不足时,系统也不会考虑将其终止。前台服务必须为状态栏提供通知,将其放在运行中的标题下方。这意味着除非将服务停止或从前台移除,否则不能清除该通知。 在 Android 8.0(API 级别 26)及更高版本中,系统对后台

    2024年02月03日
    浏览(33)
  • Android 前台服务讲解

    目录 Android 前台服务和后台服务区别 前台服务(Foreground Service): 后台服务(Background Service): 总结: 前台服务更新: JobScheduler、WorkManager 区别和使用方式 android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() 咋解决? WorkManager 在 Android 中,前台服务(Foreground

    2024年02月13日
    浏览(27)
  • Android启动前台服务(startForegroundService)

    问题: 注意事项: 8.0适配:通知需要加上NotificationChannel,开启前台服务的方式startForegroundService() 9.0适配:manifest.xml文件中需要增加权限:FOREGROUND_SERVICE Android之 Service服务详解 1、前台权限: 2、Service中开启通知: 3、启动Service: Android O对后台Service限制怎么解决 4、其他方

    2024年02月16日
    浏览(32)
  • Android14前台服务适配指南

    Android 10引入了 android:foregroundServiceType 属性,用于帮助开发者更有目的地定义前台服务。这个属性在Android 14中被强制要求,必须指定适当的前台服务类型。以下是可选择的前台服务类型: camera : 相机应用。 connectedDevice : 与连接的设备相关的应用。 dataSync : 数据同步应用。

    2024年01月22日
    浏览(32)
  • Android service(服务)中的前台服务

    紧接上文 概述 前台服务是用户主动意识到的一种服务,因此在内存不足时,系统也不会考虑将其终止。前台服务必须为状态栏提供通知,将其放在运行中的标题下方。这意味着除非将服务停止或从前台移除,否则不能清除该通知。 针对上一篇文章中的案例我们可以发现,系

    2024年02月14日
    浏览(29)
  • Android入门教程 | 四大组件之Service(前台服务,后台服务)

    Service是一种可在后台执行长时间运行操作而不提供界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可通过绑定到服务与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可在后台处理网络事务、播放

    2024年02月05日
    浏览(40)
  • Android进程类型及优先级(前台进程/可见进程/服务进程/缓存进程/空进程)

    Android 进程优先级 :前台进程 可见进程 服务进程 缓存进程 空进程 ; 关键优先级进程 : 活动进程 ; 高优先级进程 : 可见进程 , 服务进程 ; 低优先级进程 : 后台进程 , 空进程 ; Android 系统中会尽量保证优先级高的进程的存在时间尽可能长 ;如果资源不足 ( 这里的资源最主要的是内

    2024年04月12日
    浏览(23)
  • Android 应用进程保活方案实战

    前台服务:将应用运行的服务设置为前台服务,让用户知道应用正在后台运行,系统会给予一定的优先级,减少被系统杀掉的概率。但是需要注意,使用前台服务保活不能大量占用用户的通知栏,否则用户可能会感到烦躁而卸载应用。 JobScheduler:Android 5.0 开始引入的一种调度

    2024年02月15日
    浏览(32)
  • 2023Android白名单保活(后台定位)分享

    Android 系统已经更新到13了,各个Rom厂商也控制越来越严格了,还能做保活App。答案肯定是可以的,然而路线是很艰难的。 最近接到一个项目,需要安装一次app后,就需要一直获取定位。随着Android系统的不断完善,厂商rom的不断优化,想要实现后台不断定位的功能,要面临的

    2024年02月08日
    浏览(83)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包