首页 技术 正文
技术 2022年11月19日
0 收藏 553 点赞 4,519 浏览 3188 个字

Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理。

从Android官方文档中,我们知道onStartCommand有4种int返回值,首先简单地讲讲int返回值的作用。

一、onStartCommand有4种返回值:

START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。

START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。

START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。

START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

二、创建不被杀死的service

1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY(或START_STICKY_COMPATIBILITY)是service被kill掉后自动重写创建

@Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
  return START_STICKY_COMPATIBILITY;
  //return super.onStartCommand(intent, flags, startId);
 }

@Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
  flags = START_STICKY;
  return super.onStartCommand(intent, flags, startId);
  // return START_REDELIVER_INTENT;
 }

@Override
public void onStart(Intent intent, int startId)
{
// 再次动态注册广播
IntentFilter localIntentFilter = new IntentFilter(“android.intent.action.USER_PRESENT”);
localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值
myReceiver searchReceiver = new myReceiver();
registerReceiver(searchReceiver, localIntentFilter);

super.onStart(intent, startId);
}

2.在Service的onDestroy()中重启Service.

public void onDestroy()
 {
  Intent localIntent = new Intent();
  localIntent.setClass(this, MyService.class); // 销毁时重新启动Service
  this.startService(localIntent);
 }

3.创建一个广播

public class myReceiver extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent)
 {
  context.startService(new Intent(context, Google.class));
 }
}

4.AndroidManifest.xml中注册广播myReceiver及MyService服务

<receiver android:name=”.myReceiver” >
            <intent-filter android:priority=”2147483647″ ><!–优先级加最高–>
                <!– 系统启动完成后会调用 –>
                <action android:name=”android.intent.action.BOOT_COMPLETED” />              
                <!– 解锁完成后会调用 –>
                <action android:name=”android.intent.action.USER_PRESENT” />
                <!– 监听情景切换 –>
                <action android:name=”android.media.RINGER_MODE_CHANGED” />              
            </intent-filter>
</receiver>

<service android:name=”.MyService” >

注:解锁,启动,切换场景激活广播需加权限,如启动完成,及手机机状态等。

<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />

亲测ZTE U795手机Android 4.0.4版本adb push到system\app下android:persistent=”true”
变成核心程序,在360杀掉进程的时候,myReceiver照样有效,保证service重生。呃

KILL问题:
1. settings 中stop service
onDestroy方法中,调用startService进行Service的重启。
2.settings中force stop 应用
捕捉系统进行广播(action为android.intent.action.PACKAGE_RESTARTED)
3. 借助第三方应用kill掉running task
提升service的优先级,程序签名,或adb push到system\app下等

相较于/data/app下的应用,放在/system/app下的应用享受更多的特权,比如若在其Manifest.xml文件中设置persistent属性为true,则可使其免受out-of-memory killer的影响。如应用程序’Phone’的AndroidManifest.xml文件:
    <application android:name=”PhoneApp”
                 android:persistent=”true”
                 android:label=”@string/dialerIconLabel”
                 android:icon=”@drawable/ic_launcher_phone”>
         …
    </application>
设置后app提升为系统核心级别

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,895