Android 监听系统启动完毕事件

 

For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events). At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.

Step 1: First you'll need to create a simple service, defined in Monitor.java:

public class Monitor extends Service { 
 
private static final    String              LOG_TAG = "::Monitor"; 
 
@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.e(LOG_TAG, "Service created."); 
} 
 
@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    Log.e(LOG_TAG, "Service started."); 
} 
@Override 
public void onDestroy() { 
       super.onDestroy(); 
       Log.e(LOG_TAG, "Service destroyed."); 
} 
 
@Override 
public IBinder onBind(Intent intent) { 
    Log.e(LOG_TAG, "Service bind."); 
    return null; 
} 

}

Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:

  public class StartAtBootServiceReceiver extends BroadcastReceiver 
    { 
  private static final  String  LOG_TAG=StartAtBootServiceReceiver"; 
  @Override 
  public void onReceive(Context context, Intent intent) 
  { 
        Log.e(LOG_TAG, "onReceive:"); 
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
        Intent i = new Intent(); 
        i.setAction("test.package.Monitor"); 
        context.startService(i); 
    } 
} 

}

Step 3: Finally, your AndroidManifest.xml file must contain the following:

 <?xml version="1.0" encoding="utf-8"?> 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.package.Monitor" 
    android:versionName="1.0" 
    android:versionCode="100" 
    android:installLocation="internalOnly"> 
    <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> 
 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
 
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/> 
 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <service android:name="test.package.Monitor">** 
        <intent-filter> 
            <action android:name="test.package.Monitor"> 
            </action> 
        </intent-filter> 
    </service> 
    <receiver android:name="test.package.StartAtBootServiceReceiver"> 
        <intent-filter> 
            <action android:name="android.intent.action.BOOT_COMPLETED"> 
            </action> 
            <category android:name="android.intent.category.HOME"> 
            </category> 
        </intent-filter> 
    </receiver> 
</application> 

I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值