如何在android中使用Broadcast接收器调用接口

时间:2012-12-13 12:42:45

标签: android broadcastreceiver

我想调用一个警报,使用广播接收器

在修复时调用此方法
/**
 * 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();

}

删除所有邮件,但它没有发生PLZ帮助我 提前谢谢

/ **      *删除所有邮件。 0 - 已成功删除。 1 - 没有什么可以做的      *已删除。      * /

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;
        }
    }
};

我正在使用这个广播接收器

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();

如你所说我创造了一项活动

public class Activity extends PendingMessages实现了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;
    }
}

}

然后通过

调用它

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

但它不起作用然后我通过

调用它

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

然后它的力量关闭 我没有得到任何帮助,请让我纠正 提前谢谢

2 个答案:

答案 0 :(得分:1)

您无法从BroadCast接收器显示UI。

将上述代码放入活动中并从BroadcastReciever

启动活动

编辑:使用

context.startActivity(intent);

开始活动

答案 1 :(得分:1)

实际上问题是我想在后台运行UI,但在Android中却无法实现。然后我用Service在后台运行