【Android】开机启动 Service

来源:互联网 发布:协同oa平台软件 编辑:程序博客网 时间:2024/06/11 09:25
package com.lianli.JZX311;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//开机广播
public class BootBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context,BackService.class);  
        context.startService(service);  
        Log.i("开机""开机自动服务自动启动");  
    }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lianli.JZX311"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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" >
        <service
            android:name="com.lianli.JZX311.BackService"
            android:enabled="true" />
        <receiver android:name="BootBroadcastReceiver">  
                <intent-filter>  
                    <action android:name="android.intent.action.BOOT_COMPLETED"></action>  
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
        </receiver>  
        <activity
            android:name="com.lianli.JZX311.StartActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
0 0
原创粉丝点击