Android实现开机自启动Service
2010-06-22 02:26:00 来源:WEB开发网首先做一个监听器:
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);
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接