编辑预定的待定意向
Editing scheduled pending intends
我写了一个应用程序来在之前选择的预定时间打开 on/off WiFi。
它的工作方式非常简单:从时间选择器中选择时间,然后添加它。它以编程方式从时间选择器获取数据并设置和警报。
我会先为我的 activity 和广播接收器写下代码,在这段代码下面我会写下我的问题。别担心,我在代码中添加了注释,以便更清楚地阅读和理解
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
}
Wifi服务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 个 TODO:
1. 我想创建 1 个或多个警报而不替换现有的警报,我所要做的就是在每次设置警报时设置挂起标志以更新?
2. 如果我创建了例如 3 个闹钟,我想取消其中一个,指定一个。无论如何,我会将这些警报存储在数据库中,所以我的想法是:
创建警报 --> 将其添加到数据库中 --> 显示在列表中 -->
如果删除 --> 从列表中删除 --> 停用
3. 编辑报警。我知道如何编辑数据库中的项目,但如何编辑预定的警报?我的意思是编辑它的开火时间。都是为了重新创建警报吗?
有人可以回答我的问题吗?提前致谢。
设置闹钟时,将 PendingIntent
传递给 AlarmManager
。 PendingIntent
包裹了一个 Intent
。当您设置闹钟时,AlarmManager
会删除它已安排的与 PendingIntent
相匹配的所有闹钟。要确定 PendingIntent
是否匹配,将比较以下内容:
PendingIntent.getBroadcast()
调用中的requestCode
Intent
中的ACTION
Intent
中的类别
Intent
中的数据
Component
(包名,class名称)在Intent
注意:Intent
中的"extras"没有比较
所以,如果我们想重新安排一个闹钟,你只需要创建一个 PendingIntent
与之前的 requestCode
、ACTION 和 Component
相同并设置闹钟再次。 AlarmManager
将删除前一个并设置新的。
如果你想同时安排多个闹钟,你需要确保requestCode
s、Component
s或ACTIONs不同,否则它们会相互覆盖。
我写了一个应用程序来在之前选择的预定时间打开 on/off WiFi。 它的工作方式非常简单:从时间选择器中选择时间,然后添加它。它以编程方式从时间选择器获取数据并设置和警报。 我会先为我的 activity 和广播接收器写下代码,在这段代码下面我会写下我的问题。别担心,我在代码中添加了注释,以便更清楚地阅读和理解
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
}
Wifi服务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 个 TODO: 1. 我想创建 1 个或多个警报而不替换现有的警报,我所要做的就是在每次设置警报时设置挂起标志以更新? 2. 如果我创建了例如 3 个闹钟,我想取消其中一个,指定一个。无论如何,我会将这些警报存储在数据库中,所以我的想法是: 创建警报 --> 将其添加到数据库中 --> 显示在列表中 --> 如果删除 --> 从列表中删除 --> 停用 3. 编辑报警。我知道如何编辑数据库中的项目,但如何编辑预定的警报?我的意思是编辑它的开火时间。都是为了重新创建警报吗?
有人可以回答我的问题吗?提前致谢。
设置闹钟时,将 PendingIntent
传递给 AlarmManager
。 PendingIntent
包裹了一个 Intent
。当您设置闹钟时,AlarmManager
会删除它已安排的与 PendingIntent
相匹配的所有闹钟。要确定 PendingIntent
是否匹配,将比较以下内容:
PendingIntent.getBroadcast()
调用中的requestCode
Intent
中的ACTION
Intent
中的类别
Intent
中的数据
Component
(包名,class名称)在Intent
注意:Intent
中的"extras"没有比较
所以,如果我们想重新安排一个闹钟,你只需要创建一个 PendingIntent
与之前的 requestCode
、ACTION 和 Component
相同并设置闹钟再次。 AlarmManager
将删除前一个并设置新的。
如果你想同时安排多个闹钟,你需要确保requestCode
s、Component
s或ACTIONs不同,否则它们会相互覆盖。