美文网首页
AlarmManager开启一个后台servicee

AlarmManager开启一个后台servicee

作者: 小_番茄 | 来源:发表于2018-07-13 14:04 被阅读0次

    一:配合service一起简单使用,开启一个后台定时任务


    思路简单说明:

    • 开启一个不重复的定时闹钟任务
    • 任务的功能是去开启一个服务
    • 服务里面去做一些需要的操作,比如说发送一个网络请求
    • 网络请求完毕后,再次开启这个定时闹钟任务
      从而达到一个循环的定时任务效果。

    第一步:创建一个service

    public class AlarmService extends IntentService {
        public AlarmService() {
            super("AlarmService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            try {
                Thread.sleep(1000);//此处模拟发送一个网络请求
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d("AlarmService", "executed at " + new Date().toString());
            //请求结束后在开启一个闹钟
            AlarmManager.getInstance().startAlarm(this);
        }
    }
    

    第二步:创建一个AlarmReceiver

    public class AlarmReceiver extends BroadcastReceiver {
        public AlarmReceiver() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
    //        收到广播后开启服务
            Intent i = new Intent(context, AlarmService.class);
            context.startService(i);
        }
    }
    

    第三步:创建一个开启闹钟的管理者

    public class AlarmManager {
        private static AlarmManager manager;
    
        public static AlarmManager getInstance() {
            if (manager == null) {
                manager = new AlarmManager();
            }
            return manager;
        }
    
        public void startAlarm(Context context) {
            android.app.AlarmManager manager = (android.app.AlarmManager) context.getSystemService(ALARM_SERVICE);
    //        int time = 60 * 60 * 1000;   // 这是一小时的毫秒数
            int time = 5 * 1000;
            long triggerAtTime = SystemClock.elapsedRealtime() + time;
            Intent i = new Intent(context, AlarmReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            manager.set(android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        }
    
    }
    
    

    第四步:开启这个定时任务,可以启用这个服务开启,也可以通过调用AlarmManager去开启。

    public class AlarmActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_alarm);
            findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlarmManager.getInstance().startAlarm(AlarmActivity.this);
                }
            });
    
    
            findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(AlarmActivity.this, AlarmService.class);
                    startService(intent);
                }
            });
    
        }
    }
    

    运行结果

    07-13 14:19:50.865 2811-3774/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:19:50 GMT+08:00 2018
    07-13 14:19:56.876 2811-3878/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:19:56 GMT+08:00 2018
    07-13 14:20:02.887 2811-3931/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:02 GMT+08:00 2018
    07-13 14:20:08.900 2811-3969/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:08 GMT+08:00 2018
    07-13 14:20:14.928 2811-3982/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:14 GMT+08:00 2018
    07-13 14:20:20.962 2811-4001/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:20 GMT+08:00 2018
    07-13 14:20:26.992 2811-4002/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:26 GMT+08:00 2018
    07-13 14:20:33.024 2811-4019/com.tomato.soapplication D/AlarmService: executed at Fri Jul 13 14:20:33 GMT+08:00 2018
    

    相关文章

      网友评论

          本文标题:AlarmManager开启一个后台servicee

          本文链接:https://www.haomeiwen.com/subject/pxbjpftx.html