标签:
『2』『3』实现闭环循环。
07-13 10:44:45.461 10265-18533/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:44:45 GMT+08:00 2015
07-13 10:44:50.522 10265-18637/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:44:50 GMT+08:00 2015
07-13 10:44:55.545 10265-18774/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:44:55 GMT+08:00 2015
07-13 10:45:00.587 10265-18900/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:00 GMT+08:00 2015
07-13 10:45:05.617 10265-18998/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:05 GMT+08:00 2015
07-13 10:45:10.640 10265-19104/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:10 GMT+08:00 2015
07-13 10:45:15.682 10265-19198/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:15 GMT+08:00 2015
07-13 10:45:20.734 10265-19299/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:20 GMT+08:00 2015
07-13 10:45:25.765 10265-19410/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:25 GMT+08:00 2015
07-13 10:45:30.813 10265-19525/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:30 GMT+08:00 2015
07-13 10:45:35.847 10265-19547/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:35 GMT+08:00 2015
07-13 10:45:40.899 10265-19572/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:40 GMT+08:00 2015
07-13 10:45:45.917 10265-19610/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:45 GMT+08:00 2015
07-13 10:45:50.959 10265-19658/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:50 GMT+08:00 2015
07-13 10:45:55.986 10265-19682/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:45:55 GMT+08:00 2015
07-13 10:46:01.036 10265-19723/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:46:01 GMT+08:00 2015
07-13 10:46:06.059 10265-19734/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:46:06 GMT+08:00 2015
07-13 10:46:11.112 10265-19755/cf.icbd.aboutservice D/CBD﹕ LongRunningService:Mon Jul 13 10:46:11 GMT+08:00 2015
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this , LongRunningService.class) ;
startService(intent) ;
}
}
public class LongRunningService extends Service {
public LongRunningService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(
new Runnable() {
@Override
public void run() {
Log.d("CBD","LongRunningService:"+new Date().toString()) ;
}
}
).start();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE) ;
Intent intent1 = new Intent(this, AlarmReceiver.class) ; //触发广播,广播回调此方法,实现循环
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent1,0) ;
long triggerTime = SystemClock.elapsedRealtime() + 5*1000 ; //每隔5秒触发一次
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerTime,pendingIntent);
return super.onStartCommand(intent, flags, startId);
}
}
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, LongRunningService.class) ;
context.startService(i) ;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u010713545/article/details/46859645