0

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.

2 Answers2

0

You should consider using these broadcasts only inside the original Activity, since you can leak memory if you don't unregister your BroadcastReceiver in the same Activity.

If you want to receive broadcast events in the background you should consider using a service as indicated here.

You can check a more complete answer here.

Community
  • 1
  • 1
Diego Malone
  • 1,044
  • 9
  • 25
  • Hi, thank you for replying...can service perform an intent filter action? if can...how to do it? any example? –  Jul 13 '16 at 18:04
  • The documentation is already linked on the answer, but you can check a more practical example [here](http://stackoverflow.com/questions/9092134/broadcast-receiver-within-a-service/9092277#9092277). – Diego Malone Jul 13 '16 at 18:06
  • isn't the same if i use service? i still need to create service for every alarm created –  Jul 13 '16 at 18:45
0

If you're navigating away from an activity you should make sure you clean it up appropriately by overriding the OnStop, OnPause, OnDestroy... methods appropriately to handle these events properly.

As a general rule, all components of your app (including non-UI components) should clean up after themselves, not leave it to other components to clean up for them.