Android面试题-Service不死之身

作者: 小怪兽打葫芦娃 | 来源:发表于2017-05-20 17:26 被阅读2050次

Android程序员面试宝典

自定义控件

联网

工具

数据库

源码分析相关面试题

Activity相关面试题

Service相关面试题

与XMPP相关面试题

与性能优化相关面试题

与登录相关面试题

与开发相关面试题

与人事相关面试题

一: 在onStartCommand方法中将flag设置为START_STICKY;

return Service.START_STICKY;

二:在xml中设置了android:priority

 <!--设置服务的优先级为MAX_VALUE-->
 <service android:name=".MyService"
          android:priority="2147483647"
          >
 </service>

三:在onStartCommand方法中设置为前台进程

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
      Notification notification = new Notification(R.mipmap.ic_launcher, "服务正在运行",System.currentTimeMillis());
       Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent,0);
        RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification);
        remoteView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
        remoteView.setTextViewText(R.id.text , "Hello,this message is in a custom expanded view");
        notification.contentView = remoteView;
        notification.contentIntent = pendingIntent;
        startForeground(1, notification);
        return Service.START_STICKY;

    }

四:在onDestroy方法中重启service

@Override
public void onDestroy() {
        super.onDestroy();
        startService(new Intent(this, MyService.class));
}

五:用AlarmManager.setRepeating(...)方法循环发送闹钟广播,接收的时候调用service的onstart方法

Intent intent = new Intent(MainActivity.this,MyAlarmReciver.class);
        PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this, 0, intent, 0);

        // We want the alarm to go off 10 seconds from now.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 1);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        //重复闹钟
        /**
         *  @param type
         * @param triggerAtMillis t 闹钟的第一次执行时间,以毫秒为单位
         * go off, using the appropriate clock (depending on the alarm type).
         * @param intervalMillis 表示两次闹钟执行的间隔时间,也是以毫秒为单位
         * of the alarm.
         * @param operation 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等
         */
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2 * 1000, sender);

五:目前市场面的很多三方的消息推送SDK唤醒APP,例如Jpush

总结

这纯粹是面试的时候忽悠一下面试官,不代表着你的Service就永生不死了,只能说是提高了进程的优先级。迄今为止我没有发现能够通过常规方法达到流氓需求(通过长按home键清除都清除不掉)的方法,目前所有方法都是指通过Android的内存回收机制和普通的第三方内存清除等手段后仍然保持运行的方法,有些手机厂商把这些知名的app放入了自己的白名单中,保证了进程不死来提高用户体验(如微信、QQ、陌陌都在小米的白名单中)。如果从白名单中移除,他们终究还是和普通app一样躲避不了被杀的命运。

  • 欢迎关注微信公众号,长期推荐技术文章和技术视频

  • 微信公众号名称:Android干货程序员

相关文章

  • Android面试题-Service不死之身

    Android程序员面试宝典 自定义控件 一分钟实现贴纸功能 一分钟实现TextView高亮 一分钟实现新手引导页...

  • Service

    Android 面试题之Service startService intent = new Intent(Serv...

  • Android面试题整理(二)

    Android面试题整理(二) 1.Service的相关知识 Service默认并不会运行在子线程中,它也不会运行...

  • Android面试一天一题(1 Day):service

    面试题:知道Service吗,它有几种启动方式? Service是一个专门在后台处理长时间任务的Android组件...

  • 不死之身

    今天在某公众号上发了一篇关于死亡与永生的投稿,灵感来自最近那个选择安乐死的104岁澳大利亚科学家。那个工作上的公众...

  • 不死之身

    看了罗永浩和罗胖的访谈,谈到罗永浩创业所害怕的东西,讲到了初期、中期,到现在慢慢的不惧怕。 因为一路走过来,团队的...

  • 不死之身

    一个约稿。 十五六岁就是青春期最好的时候,大部分人回忆起来总是会有一些小情愫的, 或是高兴 、或是无奈、或是可笑、...

  • 不死之身

    你有没有想过,如果拥有一个不死之身,你会做些什么? 秦始皇登基之后便特别热衷于长生不老之术,后命徐福寻求长生不老仙...

  • 不死之身

    1, 牛肉馆打烊了,老牛正要关店门,忽见一老者,半人半鬼,枯坐在轮椅上,口水滴答。 闸门下面有个小坎,他的轮椅进不...

  • 不死之身

    不想死的林鹤,最终还是被一枪毙命。所有的遗憾与不舍,最终都化作虚无。 林鹤是魁国人,在被执行死刑后,椰国不敢对外公...

网友评论

本文标题:Android面试题-Service不死之身

本文链接:https://www.haomeiwen.com/subject/dbypxxtx.html