无法通过broadcastReceiver调用方法
我想通过传递一个方法(postMessage)broadcastReceiver传递上下文,但不工作。无法通过broadcastReceiver调用方法
什么是错误?我尝试了很多东西,但仍然没有工作。
public class AlarmReceiver extends BroadcastReceiver implements
OnActivityResultListener {
public void onReceive(Context context, Intent intent) {
try {
controler(context);
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void controler(Context context) {
String radioButtonName = MAinActivity.actionAlarmName(radioButtonName);
if (radioButtonName.equals("1")) {
// TODO
} else if (radioButtonName.equals("2")) {
postMessage(context);
}
}
public void postMessage(Context context) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
// No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
如果王某没有表现出活动场景中接收一个对话框,您可以在此:
dialog.getWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
不要忘记在AndroidManifest添加权限。 XML。
在AndroidManifest
添加您的接收器
<receiver android:name=".AlarmReceiver" android:process=":remote" />
我已经将接收器添加到清单文件,但仍然无法工作。 – user1207965 2013-02-26 03:47:26
分享你的方法来设置闹钟。 – ihsanhf 2013-02-26 05:06:09
的Context
您在BroadcastReceiver
的onReceive()
接收不活动上下文,它的应用环境。您不能使用应用程序上下文显示对话框。对话框应始终与Activity
关联。
正如Android官方文档中所述您无法在接收方法中显示对话框。
即使你要显示一个对话框,那么在哪里用户开始对话风格或活动 你就可以开始一个透明的活动,它会显示警告对话框在其上创建方法的一种替代方法。
希望这会有所帮助:
是的,它的工作原理,谢谢。 – user1207965 2013-03-02 03:40:53