zoukankan      html  css  js  c++  java
  • 后台执行的定时任务

    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity{
        private Button startIntentService;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent it=new Intent(this,LongRunningService.class);
            startService(it);
        }
    }
    package com.pingyijinren.test;
    
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.SystemClock;
    import android.util.Log;
    
    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("MainActivity","我是子线程");
                }
            }).start();
    
            AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
            long triggerAtTime= SystemClock.elapsedRealtime()+10*1000;
            Intent it=new Intent(this,AlarmReceiver.class);
            PendingIntent pi=PendingIntent.getBroadcast(this,0,it,0);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
            return super.onStartCommand(intent,flags,startId);
        }
    }
    package com.pingyijinren.test;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class AlarmReceiver extends BroadcastReceiver {
        public AlarmReceiver() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
            //throw new UnsupportedOperationException("Not yet implemented");
            Intent it=new Intent(context,LongRunningService.class);
            context.startService(it);
        }
    }
  • 相关阅读:
    Android studio ocr初级app开发问题汇总(含工程代码)
    面试题思路,25匹马 5个跑道取前三,前五,最少比赛次数--七次
    中国计算机学会推荐国际学术会议和期刊目录(CCF)2019年
    swift 数组部分排序
    SceneKit下关于修改SCNNode 的Shader展示自定义图形
    汇编语言实现字母下落效果
    组合数计算-java
    miniblast_hash算法c语言实现
    日常开发知识点备忘
    骑士周游问题跳马问题C#实现(附带WPF工程代码)
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5506258.html
Copyright © 2011-2022 走看看