关闭

Android 监听系统启动完毕事件

695人阅读 评论(0) 收藏 举报
 

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:526633次
    • 积分:6625
    • 等级:
    • 排名:第3718名
    • 原创:97篇
    • 转载:288篇
    • 译文:0篇
    • 评论:5条
    文章分类