Android实现开机自启动Service
来源:互联网 发布:爱淘宝退款红包会退吗 编辑:程序博客网 时间:2024/05/19 02:25
首先做一个监听器:
Java代码:
- public class StartBroadcastReceiver extends BroadcastReceiver{
- private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION)){
- Intent i= new Intent(Intent.ACTION_RUN);
- i.setClass(context, TService.class);
- context.startService(i);
- }
- }
- }
然后再做一个service:
Java代码:
- package com.testService;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.Handler;
- import android.os.IBinder;
- import android.util.Log;
- public class TService extends Service {
- /**
- * 创建Handler对象,作为进程传递postDelayed之用
- */
- private Handler objHandler = new Handler();
- private int intCounter = 0;
- private static final String TAG = "TService";
- private NotificationManager notificationManager;
- private Runnable mTasks = new Runnable() {
- public void run() {
- intCounter++;
- Log.i("HIPPO", "Counter:" + Integer.toString(intCounter));
- objHandler.postDelayed(mTasks, 1000);
- }
- };
- public void onCreate() {
- Log.d(TAG, "============> TService.onCreate");
- notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- showNotification();
- super.onCreate();
- }
- public void onStart(Intent intent, int startId) {
- Log.i(TAG, "============> TService.onStart");
- objHandler.postDelayed(mTasks, 1000);
- super.onStart(intent, startId);
- }
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "============> TService.onBind");
- return null;
- }
- public class LocalBinder extends Binder {
- public TService getService() {
- return TService.this;
- }
- }
- public boolean onUnbind(Intent intent) {
- Log.i(TAG, "============> TService.onUnbind");
- return false;
- }
- public void onRebind(Intent intent) {
- Log.i(TAG, "============> TService.onRebind");
- }
- public void onDestroy() {
- Log.i(TAG, "============> TService.onDestroy");
- notificationManager.cancel(R.string.service_start);
- objHandler.removeCallbacks(mTasks);
- super.onDestroy();
- }
- private void showNotification() {
- Notification notification = new Notification(R.drawable.icon,
- "SERVICE START", System.currentTimeMillis());
- Intent intent = new Intent(this, testService.class);
- intent.putExtra("FLG", 1);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
- notification.setLatestEventInfo(this, "SERVICE", "SERVICE START",contentIntent);
- notificationManager.notify(R.string.service_start, notification);
- }
- }
做一个主程序:
Java代码:
- package com.testService;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class testService extends Activity {
- private Button button01;
- private Button button02;
- private int flg;
- private Intent tsIntent;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button01 = (Button)findViewById(R.id.Button01);
- button02 = (Button)findViewById(R.id.Button02);
- tsIntent = this.getIntent();
- Bundle bundle = tsIntent.getExtras();
- if (bundle == null){
- flg = 1;
- DisplayToast(this,"Service Start",2);
- startService();
- finish();
- }
- else{
- DisplayToast(this,"Service Stop",2);
- stopService();
- finish();
- }
- button01.setOnClickListener(Listener01);
- button02.setOnClickListener(Listener02);
- }
- Button.OnClickListener Listener01 = new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- startService();
- }
- };
- Button.OnClickListener Listener02 = new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- stopService();
- }
- };
- private void startService() {
- Intent i = new Intent(testService.this, TService.class);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- this.startService(i);
- }
- private void stopService() {
- Intent i = new Intent(testService.this, TService.class);
- this.stopService(i);
- }
- public static void DisplayToast(Context context , String str , int time){
- if (time == 1){
- //短时间显示Toast
- Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
- }
- else if (time == 2){
- //长时间显示Toast
- Toast.makeText(context, str, Toast.LENGTH_LONG).show();
- }
- }
- }
不要忘了在androidmenfest.xml中注册一下接受广播器和服务以及开通权限:
接收广播器:
Java代码:
- < receiver android:name=".StartBroadcastReceiver">
- < intent-filter>
- < action android:name="android.intent.action.BOOT_COMPLETED"/>
- < /intent-filter>
- < /receiver>
服务:
Java代码:
- < service android:name=".TService"
- android:label="TService"
- android:icon="@drawable/icon"
- android:enabled="true"
- android:exported="true"
- android:process=":remote">
- < /service>
注册权限:
Java代码:
- < uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- Android实现开机自启动Service
- Android实现开机自启动Service
- Android实现开机自启动Service
- Android 实现开机自启动 Service
- Android实现开机自启动Service
- Android实现开机自启动Service
- Android实现开机自启动Service
- Android如何实现开机自启动Service
- 【Android】开机自启动Service
- 【Android】开机自启动Service
- 【Android】开机自启动Service .
- Android Service 开机自启动
- 【Android】开机自启动Service
- android开机自启动service
- android开机自启动,service
- android开机自启动的后台Service的实现
- android开机自启动的后台Service的实现 .
- android开机自启动的后台Service的实现
- Android控制台命令am简介
- 2012.7.25计划并7.24总结
- 关于java.security.AccessControlException: access denied 的解决方法
- 在校大学生谈项目管理
- 2012.7.25计划
- Android实现开机自启动Service
- 我喜欢你......第二种喜欢
- 2012.7.23 单词 Mind the equality gap!
- 在Eclipse关闭tomcat5.5的时候就会出现Failed shutdown of Apache Portable Runtime的问题的解决方法
- HDU-2717-Catch That Cow
- android文件管理实现源码
- android 多媒体和相机详解十
- 写代码吧
- Hibernate4.1.2测试实验