android开机启动Service

package com.fei.bootstart;

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

/**
 * @author yxf
 * @Description 
 * @date 2016-3-9 上午11:00:59
 */
public class BootCompletedReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
			Toast.makeText(context, "开机启动成功", 1).show();
			   Intent newIntent = new Intent(context,StartService.class);
			   context.startService(newIntent);
			  } 
		if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
			Toast.makeText(context, "sd卡装载成功", 1).show();
			Intent newIntent = new Intent(context,StartService2.class);
			context.startService(newIntent);
		}
	}

}

程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到该广播。所以同时要监听开机和sd卡挂载

也可在AndroidManifest.xml中强制安装到手机内存,具体方法如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fei.bootstart"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly" ><!-- 默认安装到内存中 -->

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
	<!-- 获取开机启动动作的权限permission -->
 	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <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.fei.bootstart.BootCompletedReceiver">
            <intent-filter >
                <!-- action必须添加 -->
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
            <intent-filter >
                 <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                 <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
                 <data android:scheme="file" />
            </intent-filter>
        </receiver>
        <service android:name="com.fei.bootstart.StartService"></service>
    </application>

</manifest>
package com.fei.bootstart;

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

/**
 * @author yxf
 * @Description 
 * @date 2016-3-9 上午11:00:17
 */
public class StartService extends Service{
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i("123", "onstartService");
		return super.onStartCommand(intent, flags, startId);
	}

}


小米手机是定制系统,默认是不会发送手机开机广播,如需:在手机自带的安全中心----授权管理-----自启动管理-----找到自己的应用允许就行;可以添加友情提示


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值