Android 不死的应用程序service
Posted Coding_the_world
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 不死的应用程序service相关的知识,希望对你有一定的参考价值。
原理性的东西就不多空谈了,自己根据下面我给的提示一步一步看代码,然后就能实现了、
方案1:集成第三方信鸽 (一般的杀不死,360 能杀死)
方案2: 自己有这方面的一个开发需求,所以这里就贴一下类
/**统计步数后台service
* Created by $杜建峰 on 2016/7/7.
*/
public class ExerciseService extends Service
Timer timer=new Timer();
@Nullable
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public void onCreate()
super.onCreate();
timer.schedule(new TestTask(),5000,5000);
Toast.makeText(this,"服务启动",Toast.LENGTH_SHORT).show();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
//兼容版本
return START_STICKY_COMPATIBILITY;
@Override
public void onStart(Intent intent, int startId)
// 再次动态注册广播
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT");
localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值
ServiceReceiver searchReceiver = new ServiceReceiver();
registerReceiver(searchReceiver, localIntentFilter);
super.onStart(intent, startId);
@Override
public void onDestroy()
super.onDestroy();
Intent localIntent = new Intent();
localIntent.setClass(this, ExerciseService.class);
this.startService(localIntent);
class TestTask extends TimerTask
@Override
public void run()
handler.sendEmptyMessage(1);
Handler handler=new Handler()
@Override
public void handleMessage(Message msg)
super.handleMessage(msg);
if(msg.what==1)
Toast.makeText(ExerciseService.this,"服务运行",Toast.LENGTH_SHORT).show();
;
public class ServiceReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
context.startService(new Intent(context, ExerciseService.class));
以上是关于Android 不死的应用程序service的主要内容,如果未能解决你的问题,请参考以下文章