首先,看入口程序:
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>