如何在android上安排任务

时间:2016-11-21 09:19:24

标签: java android

我有一个数据库,我需要在某一天删除,我该如何执行此任务?我发现了这个:

timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

    synchronized public void run() {

        \\ here your todo;
        }

    }}, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(1));

但我不确定它是否会在到期日之前“保存任务”。感谢

2 个答案:

答案 0 :(得分:2)

要执行这些操作,您需要使用Alaram Manager,它将在给定的特定时间后启动。

首先,您需要声明将接收这些alaram的广播接收器

public class DBActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // perform delete operation here
    }

}

其次,现在注册alaram经理

AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

    DBActionReceiver receiver = new DBActionReceiver ();
    IntentFilter filter = new IntentFilter("ALARM_ACTION");
    registerReceiver(receiver, filter);

    Intent intent = new Intent("ALARM_ACTION");
    intent.putExtra("param", "My scheduled action");
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
    // invoke broadcast after one minute of my app launch
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(1000 * 60), operation) ; 

答案 1 :(得分:0)

  

AlarmManager类可以调度重复的警报   将来会在设定点运行。 AlarmManager给出了一个   PendingIntent可在计划警报时触发。当警报响起时   触发后,注册的Intent由Android系统广播,   如果目标应用程序尚未运行,则启动它。

创建一个继承自BroadcastReceiver的类。在onReceive方法中,当BroadcastReceiver接收Intent广播时调用该方法,我们将设置运行我们任务的代码。

AlarmReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // For our recurring task, we'll just display a message
        Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();

    }

}

然后我们需要在清单文件中注册BroadcastReceiver。在清单文件中声明AlarmReceiver。

<application>
    .
    .
    <receiver android:name=".AlarmReceiver"></receiver>
    .
    .
</application>

在您的调用Activity中包含以下实例变量。

private PendingIntent pendingIntent;
private AlarmManager manager;

在onCreate()中,我们创建一个Intent,它引用我们的广播接收器类并在PendingIntent中使用它。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Retrieve a PendingIntent that will perform a broadcast
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
}

然后我们将包含设置定期警报的方法。一旦设置,警报将在每X时间后触发,这里我们用10秒钟的例子你可以简单地计算它以便每天触发它。

public void startAlarm(View view) {
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    int interval = 10000;

    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}

接下来,我们还将设置cancelAlarm()方法以在需要时停止警报。

public void cancelAlarm(View view) {
    if (manager != null) {
        manager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
    }
}