I has an activity named "AddReminder" Activity which for create an alarm. A broadcast receiver will be registered every time an alarm is created. In the other activity, all the created alarm will be show in a list view. When user click on one of the list view, user can delete the alarm. There is no problem for me to delete the alarm from the list view. However, I have no idea how to unregister the broadcast receiver from the delete alarm activity.
Button to Add alarm
@Override
public void onClick(View view) {
title_text = title.getText().toString();
desc_text = desc.getText().toString();
if(location.getSelectedItem()==null)
return;
else
place_text = location.getSelectedItem().toString();
int id = group.getCheckedRadioButtonId();
radioButton = (RadioButton)findViewById(id);
point_text = radioButton.getText().toString();
//Log.d("location", place_text);
//Toast.makeText(getApplicationContext(), title_text + " " + desc_text + " " + place_text, Toast.LENGTH_LONG).show();
if (!validateTitle()) {
return;
}
if (!validateDesc()) {
return;
}
if (!validatelocation()) {
return;
}
if(!validatePoints()){
return;
}
reminder rmd = new reminder(getApplicationContext(), title_text, desc_text, place_text, point_text);
rmd.registerReceiver();
submit(rmd);
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
finish();
}
Alarm Class
public class reminder {
private Context context;
private String title, desc, location, point;
private AlarmReceiver receiver;
public reminder(){
}
public reminder(Context context, String title, String desc, String location, String point){
this.context = context;
this.title = title;
this.desc = desc;
this.location = location;
this.point = point;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public void registerReceiver() {
receiver = new AlarmReceiver(context, title, desc, location, point);
IntentFilter intentFilter1 = new IntentFilter();
intentFilter1.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
context.registerReceiver(receiver, intentFilter1);
}
public void unregisterReceiver(int id){
context.unregisterReceiver(receiver);
}
Adapter OnItemClickListener (Other Activity)
listrmd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
position2 = id;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(R.drawable.ic_menu_manage);
builder.setTitle("Option");
builder.setAdapter(adapter2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){ //edit option
Toast.makeText(getActivity(), "Edit", Toast.LENGTH_LONG).show();
}
else{ //delete option
boolean flag = myDb.removeReminder(position2);
if(flag)
Toast.makeText(getActivity(), "Success", Toast.LENGTH_LONG).show();
//I WANT TO UNREGISTER BROADCAST RECEIVER HERE
else
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
updateList();
}
}
});
builder.show();
}
});
Please Help.