我遇到了多种解决方案,我如何取消未决意图但是无济于事。我也无法触发取消,其中我在一个点击功能中放置了代码。触发该功能的按钮是代码生成的。取消无法被解雇,它继续进入预期的接收者,这是我的代码:
package com.nyp.contactmanagement_2307;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class Alarmfinal extends Activity {
DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
TextView info;
int count = 0;
LinearLayout savedAlarm;
EditText notes;
final static int RQS_1 = 1;
String notes1;
String am;
int realhrs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarmfinal);
info = (TextView)findViewById(R.id.info);
notes = (EditText)findViewById(R.id.notes);
savedAlarm = (LinearLayout)findViewById(R.id.savedAlarm);
pickerDate = (DatePicker)findViewById(R.id.pickerdate);
pickerTime = (TimePicker)findViewById(R.id.pickertime);
Calendar now = Calendar.getInstance();
pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
buttonSetAlarm = (Button)findViewById(R.id.setalarm);
buttonSetAlarm.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// getting calender
if(count < 3){
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
int yr = 2015;
int mth = 11;
int day = 01;
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal, count);
// crafting the time and date
if(pickerTime.getCurrentHour() == 0){
realhrs = 12;
am = "am";
}else if(pickerTime.getCurrentHour() == 1){
realhrs = 1;
am = "am";
}else if(pickerTime.getCurrentHour() == 2){
realhrs = 2;
am = "am";
}else if(pickerTime.getCurrentHour() == 3){
realhrs = 3;
am = "am";
}else if(pickerTime.getCurrentHour() == 4){
realhrs = 4;
am = "am";
}else if(pickerTime.getCurrentHour() == 5){
realhrs = 5;
am = "am";
}else if(pickerTime.getCurrentHour() == 6){
realhrs = 6;
am = "am";
}else if(pickerTime.getCurrentHour() == 7){
realhrs = 7;
am = "am";
}else if(pickerTime.getCurrentHour() == 8){
realhrs = 8;
am = "am";
}else if(pickerTime.getCurrentHour() == 9){
realhrs = 9;
am = "am";
}else if(pickerTime.getCurrentHour() == 10){
realhrs = 10;
am = "am";
}else if(pickerTime.getCurrentHour() == 11){
realhrs = 11;
am = "am";
}else if(pickerTime.getCurrentHour() == 12){
realhrs = 12;
am = "pm";
}else if(pickerTime.getCurrentHour() == 13){
realhrs = 1;
am = "pm";
}else if(pickerTime.getCurrentHour() == 14){
realhrs = 2;
am = "pm";
}else if(pickerTime.getCurrentHour() == 15){
realhrs = 3;
am = "pm";
}else if(pickerTime.getCurrentHour() == 16){
realhrs = 4;
am = "pm";
}else if(pickerTime.getCurrentHour() == 17){
realhrs = 5;
am = "pm";
}else if(pickerTime.getCurrentHour() == 18){
realhrs = 6;
am = "pm";
}else if(pickerTime.getCurrentHour() == 19){
realhrs = 7;
am = "pm";
}else if(pickerTime.getCurrentHour() == 20){
realhrs = 8;
am = "pm";
}else if(pickerTime.getCurrentHour() == 21){
realhrs = 9;
am = "pm";
}else if(pickerTime.getCurrentHour() == 22){
realhrs = 10;
am = "pm";
}else if(pickerTime.getCurrentHour() == 23){
realhrs = 11;
am = "pm";
}
// generate fields
// container
final LinearLayout layout = new LinearLayout(Alarmfinal.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// notes
TextView titleView = new TextView(Alarmfinal.this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// linaer 2
LinearLayout layout2 = new LinearLayout(Alarmfinal.this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// time
TextView tvtime = new TextView(Alarmfinal.this);
tvtime.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// button to cancel alarm
Button onoff = new Button(Alarmfinal.this);
onoff.setText("cancel");
onoff.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
onoff.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savedAlarm.removeViewAt(v.getId());
/*AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), count, i,PendingIntent.FLAG_UPDATE_CURRENT);
manager.cancel(pendingIntent);
pendingIntent.cancel();*/
PendingIntent.getBroadcast(getBaseContext(),count, getIntent(), 0).cancel();
count--;
}
});
layout2.addView(tvtime);
layout2.addView(onoff);
TextView tvdate = new TextView(Alarmfinal.this);
tvdate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tvdate.setText("date :" +"/" + "/");
tvtime.setText("time : " + realhrs + " : " + pickerTime.getCurrentMinute() + am);
notes1 = notes.getText().toString();
titleView.setText("notes : "+ notes1);
// layout id
layout.setId(count);
onoff.setId(count);
layout.addView(titleView);
layout.addView(layout2);
layout.addView(tvdate);
savedAlarm.addView(layout);
count++;
}}else{
Toast.makeText(getBaseContext(), "alarm finish", Toast.LENGTH_SHORT).show();
}
}});
}
private void setAlarm(Calendar targetCal, int count){
info.setText("\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("alarmId", count);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),count, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
}
要查看的区域是点击按钮和设置警报方法
中的取消代码答案 0 :(得分:0)
嗯,你现在拥有的代码肯定不会起作用:
PendingIntent.getBroadcast(getBaseContext(),count, getIntent(), 0).cancel();
在此处使用getIntent()
正在使用Intent
开始使用的Activity
。
如果使用此PendingIntent
:
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),count, intent, 0);
您应该使用以下方法取消它:
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(), count, i, 0);
manager.cancel(pendingIntent);
pendingIntent.cancel();
如果这对您不起作用,则count
的值可能不相同。两个调用中count
的值必须相同。