Android Service 不被杀死并提高优先级
2015-07-23 14:17
423 查看
Android Service 不被杀死有两种思路,一种是将APP设置为系统应用,另一种是增强service的生命力,即使屏幕背光关闭时也能运行。
由于设置为系统应用需要root,所以一般使用后一种方法:
1.Androidmanifest.xml权限许可:-----------------------------------------------------------------
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service android:name="com.xx.MyService" ></service>
2.主Activity:----------------------------------------------------------------------------------------
private ServiceConnection conn=null;
private PowerManager.WakeLock mwl;
@Override
protected void
onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mwl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag");
mwl.acquire();//屏幕关闭后保持活动
Intent service=new Intent(this,MyService.class);
startService(service);
conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { }
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //……}
};
bindService(service, conn, BIND_AUTO_CREATE);//绑定服务
}
@Override
protected void onDestroy() {
mwl.release();//释放
unbindService(conn);//解绑
super.onDestroy();
}
------------------------------------------------------------------------------------------------------
Android Service 提高优先级:
由于设置为系统应用需要root,所以一般使用后一种方法:
1.Androidmanifest.xml权限许可:-----------------------------------------------------------------
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service android:name="com.xx.MyService" ></service>
2.主Activity:----------------------------------------------------------------------------------------
private ServiceConnection conn=null;
private PowerManager.WakeLock mwl;
@Override
protected void
onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mwl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag");
mwl.acquire();//屏幕关闭后保持活动
Intent service=new Intent(this,MyService.class);
startService(service);
conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { }
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //……}
};
bindService(service, conn, BIND_AUTO_CREATE);//绑定服务
}
@Override
protected void onDestroy() {
mwl.release();//释放
unbindService(conn);//解绑
super.onDestroy();
}
------------------------------------------------------------------------------------------------------
Android Service 提高优先级:
public class MyService extendsService{ @Override public IBinder onBind(Intent intent) { Notification notification = new Notification(R.drawable.ic_launcher, "title",System.currentTimeMillis()); PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); notification.setLatestEventInfo(this, "Service", "service is running", pintent); startForeground(0, notification);//设置最高级进程, id 为 0状态栏的 notification 将不会显示 return new MyBinder(); } class MyBinder extendsBinder{ //…… } @Override public void onDestroy() { stopForeground(true);//取消最高级进程 super.onDestroy(); } }
相关文章推荐
- Android中adb push和adb install的使用区别
- Android下载并安装APK
- android学习——应用界面编程
- Android Binder机制原理(史上最强理解,没有之一)
- 基于Ubuntu 15.04 LTS编译Android5.1.0源代码
- 【Android杂谈】安卓手机与传感器通信
- Android应用程序签名(官方文档中文版)
- Android Studio常见问题
- 启动Android Studio时,可能弹出“Fetching Android SDK component information”
- android开发动画总结二(frame animation)
- Android获取View 的坐标和屏幕高度、状态栏高度、标题栏高度
- 【转】android自动化测试之MonkeyRunner使用实例(三)
- android屏幕适配
- Android 四大组件之Service
- 逆向Android软件的步骤
- Android图片异步加载框架Android-Universal-Image-Loader
- Android -- SEGV_MAPERR,SEGV_ACCERR
- Android百度 地图打开显示某坐标的地图
- Triumph! Android Studio 1.2 Sneaks In Full Testing Support(译文)
- Android 删除再创建导致open failed: EBUSY (Device or resource busy)