Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android上安排任务_Java_Android - Fatal编程技术网

Java 如何在android上安排任务

Java 如何在android上安排任务,java,android,Java,Android,我有一个数据库,我需要在某一天删除,我如何执行这项任务?我发现: timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { synchronized public void run() { \\ here your todo; } }}, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(1)); 但我不确

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

timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

    synchronized public void run() {

        \\ here your todo;
        }

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

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

要执行这些操作,您需要使用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) ; 
AlarmManager类启用重复报警的调度,这些报警 将在将来的设定点运行。AlarmManager被授予一个 随时待命,随时待命。当警报响起时 触发后,注册意图由Android系统广播, 如果目标应用程序尚未运行,则启动该应用程序

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

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>
在onCreate()中,我们创建一个意图,该意图引用我们的广播接收器类,并在PendingEvent中使用它

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();
    }
}

中的答案将帮助您安排将来的任务。我的理解是,如果您的应用程序在任务生成之前退出,则不会执行。如果您的应用程序试图在任务运行时退出,则操作系统可能会阻止它。下面由@jitesh给出的答案可能就是您想要的答案。如果上面给出的答案不适用于您,请让我知道它也不适用
getSystemService
不存在。我正在使用Realm数据库,但我需要传递一个作为
活动的上下文,如何传递此
onReceive
?thanksIt不工作…“无法解析构造函数意图”
Intent alarm=new Intent(这是AlarmReceiver.class)
public void cancelAlarm(View view) {
    if (manager != null) {
        manager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
    }
}