Ashfaque
Ashfaque

Reputation: 1284

how to invoke an interface using Broadcast receiver in android

I want to invoke an alarm which call this method at a fix time using broadcast receiver

/**
 * Delete all messages
 */
private void performDeleteAll() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(getString(R.string.confirm_message))
            .setCancelable(false)
            .setNegativeButton(getString(R.string.confirm_no),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    })
            .setPositiveButton(getString(R.string.confirm_yes),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // delete all messages
                            mHandler.post(mDeleteAllMessages);
                            adapter.refresh();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();

}

for deleting all messages but its not happening plz help me thanks in advance

/** * Delete all messages. 0 - Successfully deleted. 1 - There is nothing to be * deleted. */

final Runnable mDeleteAllMessages = new Runnable() {
    public void run() {
        getActivity().setProgressBarIndeterminateVisibility(true);
        boolean result = false;

        int deleted = 0;

        if (adapter.getCount() == 0) {
            deleted = 1;
        } else {
            result = model.deleteAllMessages();
        }

        try {
            if (deleted == 1) {
                toastLong(R.string.no_messages_to_delete);
            } else {
                if (result) {

                    toastLong(R.string.messages_deleted);
                    showMessages();
                } else {
                    toastLong(R.string.messages_deleted_failed);
                }
            }
            getActivity().setProgressBarIndeterminateVisibility(false);
        } catch (Exception e) {
            return;
        }
    }
};

i am using this Broadcast receiver

public class AlarmReceiver extends BroadcastReceiver implements Runnable {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Import Messages Invoke ",Toast.LENGTH_LONG).show();
    //  context.startService(new Intent(context,ImportMessagesTask.class));

        MessagesModel.deleteAllMessages();{
    //   new MessagesModel().deleteAllMessages();

        Database.mMessagesContentProvider.deleteAllMessages();
        }

    //  mHandler.post(mDeleteAllMessages);
    //  adapter.refresh();

as you said i did i created an activity

public class Activity extends PendingMessages implements Runnable{

/**
 * Delete all messages. 0 - Successfully deleted. 1 - There is nothing to be
 * deleted.
 */

@Override
public void run() {
    // TODO Auto-generated method stub
    getActivity().setProgressBarIndeterminateVisibility(true);
    boolean result = false;

    int deleted = 0;

    if (adapter.getCount() == 0) {
        deleted = 1;
    } else {
        result = model.deleteAllMessages();
    }

    try {
        if (deleted == 1) {
            toastLong(R.string.no_messages_to_delete);
        } else {
            if (result) {

                toastLong(R.string.messages_deleted);
                showMessages();
            } else {
                toastLong(R.string.messages_deleted_failed);
            }
        }
        getActivity().setProgressBarIndeterminateVisibility(false);
    } catch (Exception e) {
        return;
    }
}

}

and then called it through

context.startService(new Intent(context,Activity.class));

but its not working then i called it throught

context.startActivity(new Intent(context,Activity.class));

then its force to close i am not getting any help with it plz make me correct thanks in advance

Upvotes: 2

Views: 3458

Answers (2)

Ashfaque
Ashfaque

Reputation: 1284

actually the problem was that i wanted to run a UI in background but its not possible in Android. then i used Service to run in the background

Upvotes: 1

nandeesh
nandeesh

Reputation: 24820

You cannot show UI from a BroadCast Reciever.

Put the above code in activity and launch the activity from BroadcastReciever

Edit: Use

context.startActivity(intent);

to start activity

Upvotes: 1

Related Questions