大部分应用在手机长按home键删除最近程序时,进程会被杀掉,本文将实现一个不死的service,
首先,看入口程序:
MainActivity
package where.com.notkillservicedemo;import android.app.Activity;import android.content.ComponentName;import android.content.pm.PackageManager;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        PackageManager packageManager = this.getPackageManager();        ComponentName componentName = new ComponentName(this, MainActivity.class);        //隐藏APP桌面图标并且不会在最近使用程序中找到        packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,                PackageManager.DONT_KILL_APP);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}


其中最重要的是packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
执行此代码后,应用在桌面的图标会被隐藏,且长按手机home键不会查看到该程序,所以也就无法杀死进程。当然,从设置里面还是可以强制杀死的,任何应用都不例外。
相反, packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP);
就是显示应用桌面图标并且可以长按手机home键中找到该程序,从而可以将APP进程杀死。

当然我们还要有一个服务来为我们做事,
MainService
package where.com.notkillservicedemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import java.util.Timer;import java.util.TimerTask;public class MainService extends Service {    public final static String TAG = "MainService";    private  Timer timer = new Timer();    private  class MyTask extends TimerTask {        @Override        public void run() {          Log.e(TAG,"1111");        }    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.e(TAG,"onStartCommand");        timer.schedule(new MyTask(),0,2000);        return START_NOT_STICKY;    }    @Override    public void onDestroy() {        super.onDestroy();        Log.e(TAG,"onDestroy");    }    @Override    public IBinder onBind(Intent intent) {        return null;    }}


除此之外要想保证服务一直存在,可以在用户最常按的电源键上做文章,因此我们需要一个广播接收器
BootBroadcastReceiver
package where.com.notkillservicedemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * 开机自启,按键启动 */public class BootBroadcastReceiver extends BroadcastReceiver {    public static final String TAG = BootBroadcastReceiver.class.getSimpleName();    @Override    public void onReceive(Context context, Intent intent) {        Log.e(TAG, "BootBroadcastReceiver onReceive");        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(Intent.ACTION_SCREEN_ON) ||                intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_USER_PRESENT) || android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {            Log.e(TAG, "action=" + intent.getAction());            Intent ootStartIntent = new Intent(context, MainService.class);            context.startService(ootStartIntent);        }    }}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="where.com.notkillservicedemo" >    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".MainService"/>        <receiver android:name=".BootBroadcastReceiver">            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />                <action android:name="android.media.AUDIO_BECOMING_NOISY" />                <action android:name="android.intent.action.SCREEN_ON" />                <action android:name="android.intent.action.SCREEN_OFF" />                <action android:name="android.intent.action.USER_PRESENT" />            </intent-filter>        </receiver>    </application></manifest>

更多相关文章

  1. Android程序开发之数据存储(一): 使用sqlite 进行登录注册
  2. Android程序开发的环境配置
  3. AndroidManifest.xml--android系统权限定义
  4. Android(安卓)Activity中启动另一应用程序的方法,无需类名
  5. android调用照相机拍照获取照片并做简单剪裁
  6. Android(安卓)RIL CDMA分支总结(1)
  7. android实现九宫格程序
  8. Android(安卓)ANR
  9. Eclipse 运行Android程序在虚拟机中,出现问题

随机推荐

  1. Android(安卓)8.1 锁屏界面 壁纸半透明改
  2. 23.《Android安全攻防权威指南》笔记
  3. Android下native code(C++)的编译,NDK的使用
  4. android每日一问【2011-09-17】
  5. Android(安卓)Spinner 文字居中、其下拉
  6. How to discovery memory usage on my ap
  7. Android Studio官方文档之Android Studio
  8. android log日志工具的使用
  9. Android技能树 — 树基础知识小结(一)
  10. Android 消息推送