Android 监听系统启动完毕事件
2012-03-21 09:36
465 查看
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:
}
Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:
}
Step 3: Finally, your AndroidManifest.xml file must contain the following:
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
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
相关文章推荐
- 通过Android系统日志监听程序启动事件
- Android 4.0 监听启动完成事件
- Android亮屏、暗屏、解锁、关闭系统对话的监听事件
- Android监听器实现(一)Broadcast方式监听系统事件
- 【Android】使用Intent调用系统其它程序,使用onKeyDown对音量键的监听,长按事件
- Android 改写返回键事件监听,使得back键功能类似home键,让Acitivty退至后台时不被系统销毁
- android亮屏、暗屏、解锁、关闭系统对话的监听事件
- Android 4.0 监听启动完成事件
- Android系统之事件处理子系统启动过程的学习和分析
- Android拦截、监听系统返回键事件
- Android拦截、监听系统返回键事件
- android activity监听加载完毕事件
- 立即获得下载任务完成的消息通知。Android DownloadManager通过注册一个广播监听系统的广播事件完成此操作,在创建广播时候,需要指明过滤器为:DownloadManager.ACTIO
- Android拦截、监听系统返回键事件
- android亮屏、暗屏、解锁、关闭系统对话的监听事件
- android亮屏、暗屏、解锁、关闭系统对话的监听事件
- Android监听系统输入法键盘弹出显示与隐藏事件
- Android如何监听系统输入法弹出显示事件
- android开发中Service启动BroadastService监听系统剪切板服务
- android系统通讯录源码--显示联系人添加长按事件监听