我试图让 BroadcastReceiver 运行一个 AlertDialog ,它只是跳过对 Dialog 方法的调用(并抛出 catch 异常):我的 BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
this.con = context;
try
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
intent.getExtras();
new StringBuilder();
wl.release();
String[] a ={"a","b"};
create(context, a); //The Dialog Call
setOnetimeTimer(con);
Toast.makeText(context, "Hurray!", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(context, "Error,broadcastReciver"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
方法如下create()
:(创建对话框)public static void create(Context context,String[] descriptions){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setTitle("Random String");
builder.setMessage(descriptions[rn.nextInt(2)]);
//builder.setMessage("test");
builder.setInverseBackgroundForced(false);
builder.setPositiveButton("Close",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
谢谢!编辑:不要说我把它放在一个活动中并运行这个活动,因为我不希望它打开我的应用程序,我希望它在打开的应用程序之上。