我在我的应用程序中创建了一个添加警报功能的应用程序。 我把代码用不同的活动激活警报,取消代码,警报可以在我从Tambah_To_Do_List活动设置的时间内激活,但是警报不能从Daftar_To_Do_List活动中取消。
我已尝试从stackoverflow取消android警报的相同问题,但没有人为mycode工作。
以下是Tambah_To_Do_List活动的代码:
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class Tambah_To_Do_List extends Activity {
private OperasiDatabaseToDoList oprDatabasetdl = null;
private SQLiteDatabase db = null;
private EditText txttdid;
private EditText txtact;
private DatePicker weddingdate;
private TimePicker weddingtime;
private Button btnsimpan;
private Boolean data_baru;
final static int RQS_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
String tdid ="";
String activity ="";
super.onCreate(savedInstanceState);
setContentView(R.layout.tambah_to_do_list);
oprDatabasetdl = new OperasiDatabaseToDoList(this);
db = oprDatabasetdl.getWritableDatabase();
oprDatabasetdl.createTable(db);
Intent sender = getIntent();
String status = sender.getExtras().getString("status");
if(status.equalsIgnoreCase("baru")){
data_baru = true;
} else
{
data_baru = false;
tdid = sender.getExtras().getString("tdid");
activity = sender.getExtras().getString("activity");
}
txttdid = (EditText) findViewById(R.id.txttdid);
txttdid.setText(tdid);
txtact = (EditText) findViewById(R.id.txtact);
txtact.setText(activity);
weddingdate = (DatePicker) findViewById(R.id.weddingdate);
weddingtime = (TimePicker) findViewById(R.id.weddingtime);
/** final Calendar c = Calendar.getInstance();
int days = Integer.parseInt(day);
int months = Integer.parseInt(month);
int years = Integer.parseInt(year);
c.set(Calendar.DATE, days);
c.set(Calendar.MONTH, months);
c.set(Calendar.YEAR, years); */
btnsimpan = (Button) findViewById(R.id.btnsimpan);
if(data_baru==true)
btnsimpan.setText("Simpan");
else
btnsimpan.setText("Edit");
btnsimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simpandata();
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.DAY_OF_MONTH,weddingdate.getDayOfMonth());
calSet.set(Calendar.MONTH,weddingdate.getMonth());
calSet.set(Calendar.YEAR,weddingdate.getYear());
calSet.set(Calendar.HOUR_OF_DAY, weddingtime.getCurrentHour());
calSet.set(Calendar.MINUTE, weddingtime.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}
});
}
private void setAlarm(Calendar targetCal){
Context context = getApplicationContext();
Toast.makeText(context, "\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
以下是Daftar_To_Do_List活动的代码:
package com.willis.weddingmanager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Daftar_To_Do_List extends Activity {
private OperasiDatabaseToDoList oprDatabasetdl = null;
private SQLiteDatabase db = null;
private Cursor dbCursor = null;
private Button btnhapusemua;
private Button btnhapus;
private Button btnedit;
private Button btnrefresh;
private Button btnmatikan;
final static int RQS_1 = 1;
//private LinearLayout lineartambah;
private String tdid, activity, day, month, year,hour,minute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_to_do_list);
oprDatabasetdl = new OperasiDatabaseToDoList(this);
db = oprDatabasetdl.getWritableDatabase();
oprDatabasetdl.createTable(db);
TampilkanData();
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
btnhapusemua = (Button) findViewById(R.id.btnhapusemua);
btnhapusemua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.setTitle("Konfirmasi");
dialog.setMessage("Anda yakin akan menghapus seluruh data?");
dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
oprDatabasetdl.deleteAllDataToDoList(db);
TampilkanData();
}
});
dialog.show();
}
});
btnmatikan = (Button) findViewById(R.id.btnmatikan);
btnmatikan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.setTitle("Konfirmasi Alarm");
dialog.setMessage("Anda yakin akan mematikan alarm?");
dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), RQS_1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
});
dialog.show();
}
});