Android开机自启动服务的写法

首先AndroidManifest.xml中<application>结点中要加入一段广播接收器接收name为"android.intent.action.BOOT_COMPLETED"的开机广播,其中"com.example.noemhost.BootBroadcastReceiver"就是让系统接受到广播之后通过反射调用的广播接收类:

        <!--随着安卓系统启动而启动-->
        <receiver android:name="com.example.noemhost.BootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

广播接收类的写法:

接收到启动完成的广播后,你可以利用回调给你的context上下文通过startActivity启动一个Acitivity,或者利用startService启动一个服务,这样开机时便会运行你的后台服务但是不实际显示任务于任务栏中了。

package com.example.noemhost;

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



public class BootBroadcastReceiver extends BroadcastReceiver {
    public static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            Intent welcomeIntent = new Intent(context, DetectService.class);
//            context.startActivity(welcomeIntent);
            context.startService(welcomeIntent);
        }
    }
}

一个示例服务:

package com.example.noemhost;

import android.R.interpolator;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/** 指纹搜索服务 **/
public class DetectService extends Service {

	private boolean canRun = true;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}



	private boolean firstRun = true;
	@Override
	public void onCreate() {
		super.onCreate();
		Toast.makeText(this, "WTF", Toast.LENGTH_SHORT).show();
		
	}

}

完整的AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.noemhost"
    android:versionCode="1"
    android:versionName="1.3" 
    
    >
    
    <!-- 
        android:process="system"
    	android:sharedUserId="android.uid.system"
     -->

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:logo="@drawable/ic_launcher">
        
        <service android:name="com.example.noemhost.DetectService" >
        </service>
        
        <!--android:theme="@android:style/Theme.NoDisplay" -->
        <!--  <category android:name="android.intent.category.DEFAULT" /> -->
        <activity 
            android:name="com.example.noemhost.MainActivityVer2">
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
        </activity>
        
        <!--随着安卓系统启动而启动-->
        <receiver android:name="com.example.noemhost.BootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    


</manifest>

猜你喜欢

转载自blog.csdn.net/cjzjolly/article/details/81333295