编辑计划挂起的意图



我写了一个应用程序,可以在之前选择的预定时间打开/关闭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 
}

无线网络服务等级:

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. 编辑闹钟。我知道如何编辑数据库中的项目,但是如何编辑计划警报?我的意思是编辑它的火灾时间。这一切都是为了重新创建闹钟吗?

谁能回答我的问题?提前谢谢。

设置闹钟时,会将PendingIntent传递给AlarmManagerPendingIntent包裹着Intent.设置闹钟时,AlarmManager会删除已安排的与PendingIntent匹配的任何闹钟。要确定PendingIntent是否匹配,将比较以下内容:

  • PendingIntent.getBroadcast()通话中的requestCode
  • Intent中的行动
  • Intent中的类别
  • Intent中的数据
  • Intent中的Component(包名、类名(

注意:Intent中的"额外内容"不进行比较

因此,如果我们想重新安排闹钟,您只需创建一个与上一个具有相同requestCode、ACTION 和ComponentPendingIntent,然后再次设置闹钟。AlarmManager将删除前一个并设置新的。

如果要并行调度多个警报,则需要确保requestCodeComponent或 ACTIONS 不同,否则它们会相互覆盖。

最新更新