1

私は助けが必要です。アンドロイドコーディングは初めてです。

タスクに書かれた時間に具体的にやりたいタスクリストを作りました。

ここに私のタスク項目があります

private long id;
private int mon;
private int tues;
private int wednes;
private int thurs;
private int fri;
private int satur;
private int sun;
private int profile;

分数を保持する日(月曜日、火曜日など)があります(10:00の場合、600)。

いくつかのチュートリアルに従って、アラーム受信機を持っています

public class AlarmReciever extends  BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("alarm_message");

            Intent newIntent = new Intent(context, AlarmActivity.class);
            newIntent.putExtra("profile", message);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();

        }
}

まだ未編集です…

そして、アラームマネージャーで新しいタスクを作成するために呼び出すコードがあります

// get a Calendar object with current time


Calendar cal = Calendar.getInstance();
 // add 5 minutes to the calendar object
 cal.add(Calendar.MINUTE, 5);
 Intent intent = new Intent(ctx, AlarmReceiver.class);
 intent.putExtra("alarm_message", "O'Doyle Rules!");
 // In reality, you would want to have a static variable for the request code instead of 192837
 PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 // Get the AlarmManager service
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

カレンダーで指定する方法がわかりません。たとえば、毎週月曜日の 10:00 に新しいタスクを繰り返す必要があり、これが発生すると、新しい関数を呼び出して、操作する「プロファイル」変数を与えます。

 private void setProfile(Integer profile)
{
 // Doing things with profile
}
4

1 に答える 1

0

次のコードを見てください。

    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent2 = new Intent(context, SampleAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent2, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    // Set the alarm's trigger time to item hour
    calendar.set(Calendar.HOUR_OF_DAY, NuevoItemActivity.hora.getCurrentHour());
    calendar.set(Calendar.MINUTE, NuevoItemActivity.hora.getCurrentMinute());

    // Set the alarm to fire , according to the device's clock, and to repeat once a day.
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, (PendingIntent) alarmIntent);

最後の行でわかるように、AlarmManager.INTERVAL_DAYを繰り返すように指定できますPendingIntent

于 2014-01-22T22:28:50.820 に答える