【问题标题】:Editing scheduled pending intends编辑计划的待处理意图
【发布时间】:2018-06-29 16:46:10
【问题描述】:

我编写了一个应用程序来在之前选择的预定时间打开/关闭 WiFi。 它的工作方式非常简单:从时间选择器中选择时间,然后添加它。它以编程方式从时间选择器获取数据并设置和警报。 我会先为我的活动和广播接收器写下代码,在这段代码下面我会写下我的问题。别着急,我把cmets放在代码里面是为了更清晰易懂

Wifi.class:

public class WiFi extends AppCompatActivity {
private final int TEMP_REQUEST_CODE = 1;
private AlarmManager alarmManager;
private TimePicker timePicker;
private PendingIntent pendingIntent;
private Intent intent;
private Context context;
private Calendar calendar;
Intent alarmIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wifi_class);
    timePicker = (TimePicker) findViewById(R.id.timePicker1);
    timePicker.setIs24HourView(true);
    setTitle("Set WiFi off/on");
    //store context in global variable
    context = getApplicationContext();
    //creates an intent that will be used in pending intent
    initializeView();
    //creates pending intent
    createAlarmIntent();
    //checks if alarm exists and sets matching text on the screen
    checkAlarmExists();

}

// I got an add button in my toolbar that saves the alarm, same as to delete 
an alarm
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tick:
            Alarm();
            finish();
            return true;
        case R.id.dismiss:
            cancelAlarm();
        default:
            return super.onOptionsItemSelected(item);
    }
}
//this method initialize intent and stores it into variable, 
WiFiService.class extends BroadcastReceiver
//and all what it has to do is to turn wifi on/off
private void initializeView () {
    intent = new Intent(context, WifiService.class);

    //creating global pendingIntent variable
     pendingIntent = PendingIntent.getBroadcast(context,
            0,
            intent,
            0);
    //creating global alarmManager variable
     alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
//this method gets selected time from timepicker and calls another method to 
create an alarm
private void Alarm() {
     calendar = Calendar.getInstance();
    if (Build.VERSION.SDK_INT >= 23) {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getHour(),
                timePicker.getMinute(), 0
        );

    } else {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),
                0);
    }
    setAlarm();
}

/**
 * This method sets Alarm that will be executed even in Doze mode and will 
intent WifiServices class,
 * in the result it will turn wifi off or on
 *
 */
private PendingIntent createAlarmIntent() {
     alarmIntent = new Intent(this, WifiService.class);
    return PendingIntent.getBroadcast(this, TEMP_REQUEST_CODE,
            alarmIntent, 0);
}
private void setAlarm() {
//setting alarm
    PendingIntent intent = createAlarmIntent();
    if (Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAlarmClock(new 
AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent), 
intent);
}
    else {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), 
pendingIntent);
    }

    long AlarmMgs = calendar.getTimeInMillis() - 
Calendar.getInstance().getTimeInMillis();
    Toast.makeText(context, "Alarm will be executed in " + AlarmMgs / 1000 / 
60 + "min", Toast.LENGTH_SHORT).show();

}


/**
 * This method should cancel alarm that is being currently set. There is no 
 if/else statement 
 * because it always says that alarm exists (see next method)
 */
private void cancelAlarm() {

    alarmManager.cancel(createAlarmIntent());
    Toast.makeText(context, "Alarm dismissed", Toast.LENGTH_SHORT).show();
    checkAlarmExists();
}

/**
 * This method checks wrether alarm is set or not and assigns that into 
 TextView
 * Unfortunately it always says it exists
 */
private void checkAlarmExists() {

    boolean alarmExists =
            (PendingIntent.getBroadcast(context,
                    0,
                    intent,
                    PendingIntent.FLAG_NO_CREATE)
                    != null);

    TextView alarmSetter = (TextView) findViewById(R.id.alarmSet);
    if (alarmExists) {
        alarmSetter.setText("Alarm is set");

    } else {
        alarmSetter.setText("Alarm is not set yet");
    }
}
    //TODO: (1): Create more than 1 alarm without replacing one before
    //TODO: (2): Cancel one of them, not all of them
    //TODO: (3): Edit those alarms 
}

WifiService 类:

public class WifiService extends BroadcastReceiver {

public static final String TAG = "WiFi";

@Override
public void onReceive(Context context, Intent intent) {
//        if 
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
{

        WifiManager wifiManager = (WifiManager) 
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Log.v("Wifi", "checked");
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
            Toast.makeText(context, "Turning wifi off", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "Turned off");
        } else {
            wifiManager.setWifiEnabled(true);
            Toast.makeText(context, "turning wifi on", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "turned off");
        }
//        }
    Log.v(TAG, "Works");
}

}

好的,我在代码中留下了 3 个 TODO: 1.我想创建1个或多个警报而不替换现有警报,我所要做的就是在每次设置警报时设置挂起标志以更新? 2. 如果我创建例如 3 个警报,我想取消其中一个,指定一个。无论如何,我会将这些警报存储在数据库中,所以我的想法是: 创建警报 --> 将其添加到数据库中 --> 显示在列表中 --> 如果已移除 --> 从列表中移除 --> 停用 3. 编辑警报。我知道如何编辑数据库中的项目,但如何编辑预定警报?我的意思是编辑它的着火时间。只是为了重新创建警报吗?

有人可以回答我的问题吗?提前致谢。

【问题讨论】:

    标签: android android-intent alarmmanager android-pendingintent


    【解决方案1】:

    设置闹钟时,您将PendingIntent 传递给AlarmManagerPendingIntent 包装了 Intent。当您设置警报时,AlarmManager 会删除它已经安排的与 PendingIntent 匹配的所有警报。要确定PendingIntents 是否匹配,将比较以下内容:

    • PendingIntent.getBroadcast() 调用中的requestCode
    • Intent 中的操作
    • Intent 中的类别
    • Intent 中的数据
    • Intent 中的Component(包名、类名)

    注意:Intent 中的“额外”不进行比较

    所以,如果我们想重新安排闹钟,您只需创建一个PendingIntent,与之前的requestCode、ACTION 和Component 相同,然后重新设置闹钟。 AlarmManager 将删除前一个并设置新的。

    如果您想并行安排多个警报,则需要确保requestCodes、Components 或 ACTION 不同,否则它们会相互覆盖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多