WEB开发网
开发学院手机开发Android 开发 Android实现开机自启动Service 阅读

Android实现开机自启动Service

 2010-06-22 02:26:00 来源:WEB开发网   
核心提示:首先做一个监听器:public class StartBroadcastReceiver extends BroadcastReceiver{private static final String ACTION = "android.intent.action.BOOT_COMPLETED";pub

首先做一个监听器:

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:

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);

1 2 3 4  下一页

Tags:Android 实现 开机

编辑录入:coldstar [复制链接] [打 印]
赞助商链接