码迷,mamicode.com
首页 > 移动开发 > 详细

安卓服务常驻

时间:2015-08-02 18:17:17      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:安卓   service常驻   广播   

最近研究怎样防止服务被杀死,网上百度了下,发现基本就两只设置方法,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常驻   广播   

原文地址:http://blog.csdn.net/qingchunweiliang/article/details/47208917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!