android开发分享后台定时任务(从开机起,开启一个app级HTTP服务器)

接收广播:里面开启一个服务LongRunningService;接收开机广播,也接收LongRunningService发出的广播。public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(FaceApplication.getCont

接收广播:

里面开启一个服务LongRunningService;

接收开机广播,也接收LongRunningService发出的广播。

 public class AlarmReceiver extends BroadcastReceiver {      @Override      public void onReceive(Context context, Intent intent) {          Intent i = new Intent(FaceApplication.getContext(), LongRunningService.class);          i.putExtra("type",1);          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {              context.startForegroundService(i);          } else {              context.startService(i);          }      }  } 

被开启的服务:

里面每5s发送一广播给AlarmReceiver

里面开线程执行自己想要干的任务(开启一个HTTP服务器)

 public class LongRunningService extends Service {      private MyAppServer mywebserver;        @Override      public IBinder onBind(Intent intent) {          return null;      }        @Override      public int onStartCommand(Intent intent, int flags, int startId) {            int type = intent.getIntExtra("type",1)

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/894802.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐