如何在每天上午12点安排任务

时间:2018-01-06 04:14:54

标签: android

我想每天早上12点准确启动ScheduledExecutorService,但是我的代码计划没有启动。谁能告诉我我的代码是否正确?如果有任何其他方式可以提供帮助

public class MainActivity extends AppCompatActivity{  
ScheduledExecutorService scheduler;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
scheduler = Executors.newScheduledThreadPool(1);

scheduleTask();

}

 public void scheduleTask() {

        final Runnable beeper = new Runnable() {
            public void run() {
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {


Toast.makeText(getApplicationContext(),"SCHEDULE STARTED",Toast.LENGTH_LONG).show();

                                    }
                                }
                            }
                        }, 1500);
                    }
                    });
            }
            };

        Calendar midnight = Calendar.getInstance();
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 1);
        midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
        long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
        long ticksTillMidnight = tillMidnight / 50;

        final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, ticksTillMidnight,20*60*60*24, TimeUnit.MILLISECONDS
        );
        scheduler.schedule(new Runnable() {
            public void run() {
                beeperHandle.cancel(true);
            }
        }, 6*6,     TimeUnit.MILLISECONDS
        );
    }}

1 个答案:

答案 0 :(得分:0)

答案很简单。在您的情况下,ScheduledExecutorService绑定到您的流程的生命周期。如果您的进程被杀死,那么ScheduledExecutorService将会被杀死。而是使用AlarmManager

以下是3个步骤

  1. 创建闹钟
  2. 创建BroadcastReceiver
  3. 在App Manifest中注册Receiver
  4. <强> 1。创建警报

    public void createAlarm() {
        //System request code
        int DATA_FETCHER_RC = 123;
        //Create an alarm manager
        AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    
        //Create the time of day you would like it to go off. Use a calendar
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
    
        //Create an intent that points to the receiver. The system will notify the app about the current time, and send a broadcast to the app
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, DATA_FETCHER_RC,intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        //initialize the alarm by using inexactrepeating. This allows the system to scheduler your alarm at the most efficient time around your 
        //set time, it is usually a few seconds off your requested time.
        // you can also use setExact however this is not recommended. Use this only if it must be done then.
    
        //Also set the interval using the AlarmManager constants
        mAlarmManager.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
    
    }
    

    <强> 2。创建BroadcastReceiver

    //This is the broadcast receiver you create where you place your logic once the alarm is run. Once the system realizes your alarm should be run, it will communicate to your app via the BroadcastReceiver. You must implement onReceive.
    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Your code once the alarm is set off goes here
            //You can use an intent filter to filter the specified intent
        }
    }
    

    第3。在您的应用清单中注册接收器

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package">
    //This is a useful permission as it allows your apps alarm to still be active once a reboot takes place
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <receiver
            android:name=".MainActivity$AlarmReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>