2
我遇到過多個解決方案,關於如何取消掛起的意圖,但無濟於事。我也一直無法觸發取消,其中我已經在一個點擊功能的代碼。觸發該功能的按鈕是代碼生成的。取消無法被解僱並繼續到指定的接收者,這裏是我的代碼:取消android等待意圖
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 [email protected] " + 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);
}
}
方面看都在點擊中設定的報警方法,在通斷按鈕取消代碼
啊我看到生病給它一個嘗試(:謝謝!!! –
哦,我試着做這個,但對於經理,如果我在onclick裏面創建一個局部變量Alarmmanager管理器,它是指同一個我用於設置? –
只有一個'AlarmManager',你可以在一個字段(成員變量)中存儲對'AlarmManager'的引用,或者你可以調用'getSystemService(Context.ALARM_SERVICE)'獲得它每次 –