安卓服务常驻

最近研究怎样防止服务被杀死,网上百度了下,发现基本就两只设置方法,1.在Service onStartCommand中返回super.onStartCommand(intent, START_STICKY, startId);,这样进程被一键杀死后会立即自动重启。  2.注册receiver,监听开机等事件,在onReceive中启动服务。   

在模拟器上没问题,可以正常启动,而且杀不死,但在手机上一直没法启动,后来发现是小米系统设置的问题,在安全中心中允许自动启动就行了,默认已经允许微信,qq自动启动,所以微信qq杀不死。

注意:新安装的应用要启动一次后注册的receiver才能生效,以后不管是应用被杀死,还是刚开机,都能收到解锁等广播了


附上代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kaijiqidong"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <!-- 监听开机启动权限 -->
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        
    
    <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>
        
         <receiver android:name="com.example.kaijiqidong.MyReceiver">
            <intent-filter>
                <!-- 应用卸载 -->
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <data android:scheme="package"/>
            </intent-filter>
            <intent-filter>
                <!-- 开机 -->
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <!-- 解锁 -->
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        
         <service android:name="com.example.kaijiqidong.MyService" ></service>
    </application>

</manifest>


MainActivity里面的代码可以忽略。。。

package com.example.kaijiqidong;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById(R.id.but).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startService(new Intent(getApplicationContext(), MyService.class));
				
				bindService(new Intent(getApplicationContext(), MyService.class), new ServiceConnection() {
					
					@Override
					public void onServiceDisconnected(ComponentName name) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onServiceConnected(ComponentName name, IBinder service) {
						// TODO Auto-generated method stub
						
					}
				}, BIND_AUTO_CREATE);
			}
		});
	}
}



package com.example.kaijiqidong;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("收到  ---- "+intent.getAction());
		//启动服务
		context.startService(new Intent(context, MyService.class));
		
		Toast.makeText(context, "fu wu  qi  dong", 0).show();
	}

}


package com.example.kaijiqidong;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service{

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		//返回super.onStartCommand(intent, START_STICKY, startId);
		//保证被一键清理后自动重启服务
		return super.onStartCommand(intent, START_STICKY, startId);
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Intent intent=new Intent(getApplicationContext(), MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

}




你可能感兴趣的:(安卓,广播,service常驻)