编辑预定的待定计划 [英] Editing scheduled pending intends

查看:85
本文介绍了编辑预定的待定计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个应用程序,可以在之前选择的预定时间打开/关闭WiFi。
它的工作方式非常简单:从时间选择器中选择时间,然后将其添加。它以编程方式从时间选择器获取数据并进行设置和报警。
我要为我的活动写下一个代码,然后广播给接收者,下面是这个代码,我会写我的问题。不用担心,我在代码中添加了注释,以使阅读和理解更加清晰

I wrote an app to turn on/off WiFi at scheduled time choosen before. The way it works is preety simple: Choose time from timepicker, then just add it. Programmatically it gets data from timepicker and sets and alarm. I'd write down a code for my activity and broadcast receiver first, bellow this code I'll write my questions. Don't worry, I put comments inside the code to be more clear to read and understand

Wifi.class:

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类:

WifiService class:

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个待办事项代码:
1.我想创建1个或多个警报而不替换现有警报,我要做的就是设置挂起标志以在每次设置警报时进行更新?
2.如果我创建了3个警报,我想取消其中一个,指定一个。无论如何,我都会将这些警报存储在数据库中,所以我的想法是:
创建一个警报->将其添加到数据库中->在列表中显示->
(如果已删除)- >从列表中删除->禁用
3.编辑警报。我知道如何编辑数据库中的项目,但是如何编辑预定警报?我的意思是编辑其起火时间。

Okay, I left 3 TODOs in the code: 1. I want to create 1 or more alarms without replacing existing one, all I have to do is to set pending flag to update every time when I set an alarm? 2. If I create e.g 3 alarms, I want to cancel one of them, specified one. I will store those alarms in database anyway, so my way of thinking is that: create an alarm --> add it to a database --> display in a list --> if removed --> remove from the list --> disactivate 3. Editing alarms. I know how to edit items in database, but how to edit scheduled alarm? I mean to edit its time of fire. Is it all about recreating an alarm?

有人能回答我的问题吗?

Could anyone answer for my questions? Thanks in advance.

推荐答案

设置闹钟时,您会传递 PendingIntent AlarmManager PendingIntent 包装一个 Intent 。设置警报时, AlarmManager 会删除它已计划的与 PendingIntent 匹配的任何警报。要确定 PendingIntent s是否匹配,将进行以下比较:

When you set an alarm, you pass a PendingIntent to AlarmManager. The PendingIntent wraps an Intent. When you set an alarm, AlarmManager deletes any alarms it already has scheduled that match the PendingIntent. To determine if the PendingIntents match, the following are compared:


  • PendingIntent.getBroadcast()呼叫
  • 中的code> requestCode
  • 意图

  • 意图

  • 中的类别
  • 意向
  • 中的DATA
  • 组件(程序包名称) ,类名)在 Intent

  • The requestCode in the PendingIntent.getBroadcast() call
  • The ACTION in the Intent
  • The CATEGORIES in the Intent
  • The DATA in the Intent
  • The Component (package name, class name) in the Intent

注意: 意图未进行比较

因此,如果我们要重新安排警报,则只需创建一个 PendingIntent ,具有与上一个相同的 requestCode ,ACTION和 Component 并再次设置闹钟。 AlarmManager 将删除前一个并设置新的。

So, if we want to reschedule an alarm, you just need to create a PendingIntent with the same requestCode, ACTION and Component as the previous one and set the alarm again. AlarmManager will remove the previous one and set the new one.

如果要并行安排多个警报,则需要确保 requestCode s, Component s或ACTIONs不同,否则它们将被覆盖彼此。

If you want to have multiple alarms scheduled in parallel, you need to ensure that either the requestCodes, the Components or the ACTIONs are different, otherwise they will overwrite each other.

这篇关于编辑预定的待定计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆