标签:android 安卓 broadcast broadcastreceiver
我们只需要监听一个开机启动的Broadcast(广播)即可。
首先写一个Receiver(即广播监听器),继承BroadcastReceiver,如下
public class BootReceiver extends BroadcastReceiver { private PendingIntent mAlarmSender; @Override public void onReceive(Context context, Intent intent) { // 在这里干你想干的事(启动一个Service,Activity等), //本例是启动一个定时调度程序,每30分钟启动一个Service去更新数据 mAlarmSender = PendingIntent.getService(context, 0, new Intent(context, RefreshDataService.class), 0); long firstTime = SystemClock.elapsedRealtime(); AlarmManager am = (AlarmManager) context .getSystemService(Activity.ALARM_SERVICE); am.cancel(mAlarmSender); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30 * 60 * 1000, mAlarmSender); } }
主要是接受到安卓的开机广播通知就可以 启动自己的服务或者程序了
<receiver android:name=".service.BootReceiver"> <intent-filter> <!-- 系统启动完成后会调用--> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver>
标签:android 安卓 broadcast broadcastreceiver
原文地址:http://blog.csdn.net/u012889434/article/details/43668399