Android 技巧 - 开机完成后做某事 (比如启动Service)
来源:互联网 发布:20172018nba数据库统计 编辑:程序博客网 时间:2024/04/29 18:29
It's possible to register your own application service for starting automatically when the device has been booted. You need this, for example, when you want to receive push events from a http server and want to inform the user as soon a new event occurs. The user doesn't have to start the activity manually before the service get started...
It's quite simple. First give your app the permission RECEIVE_BOOT_COMPLETED.
Next you need to register a BroadcastReveiver. We call it BootCompletedIntentReceiver.
Your Manifest.xml should now look like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> <receiver android:name=".BootCompletedIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".BackgroundService"/> </application></manifest>
As the last step you have to implement the Receiver. This receiver just starts your background service.
package com.jjoe64;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.preference.PreferenceManager;import com.jjoe64.BackgroundService;public class BootCompletedIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent pushIntent = new Intent(context, BackgroundService.class); context.startService(pushIntent); } }}
0 0
- Android 技巧 - 开机完成后做某事 (比如启动Service)
- Android开机启动Service
- Android开机启动Service
- Android开机启动Service
- 【Android】开机启动 Service
- android开机启动Service
- Android 开机启动Service
- android开机启动Service(小发现)
- Android activity/service 开机启动
- 【Android】开机自启动Service
- 【Android】开机自启动Service
- 【Android】开机自启动Service .
- Android 开机启动的service
- Android Service 开机自启动
- 【Android】开机自启动Service
- android开机自启动service
- android开机自启动,service
- Android中开机启动service
- Apple 官方教程:App Development Process
- python2.7 安装pypa-setuptools
- 查看文本文件中的内容
- Apple 官方教程:Designing a User Interface
- 移动和复制,新建和删除
- Android 技巧 - 开机完成后做某事 (比如启动Service)
- 可变个数参数的用法
- eclipse 快捷键大全
- Win7下Qt5.2中使用OpenGL的glu函数库无法使用的解决方案
- Apple 官方教程:Defining the Interaction
- Insertion Sort (Codeforces Round #212 (Div. 2))
- 【前端组件】对easy validator的个人定制版
- sql server 18456错误
- java关于ServletConfig FilterConfig什么用