Dialog 如何在BroadcastReceiver上显示当前可见活动的对话框?
我有一项主要活动(海洋智能活动)。在这个活动中,我注册了推送通知的设备,还注册了一个显示对话框的接收器,并根据服务器发送的信息启动适当的活动。这是我用来注册设备和接收器的代码:Dialog 如何在BroadcastReceiver上显示当前可见活动的对话框?,dialog,broadcastreceiver,Dialog,Broadcastreceiver,我有一项主要活动(海洋智能活动)。在这个活动中,我注册了推送通知的设备,还注册了一个显示对话框的接收器,并根据服务器发送的信息启动适当的活动。这是我用来注册设备和接收器的代码: protected void gcmRegistration(){ PMApplication thisApp = PMApplication.getInstance(); AppDelegate delegate = thisApp.getAppDelegate(); final Contex
protected void gcmRegistration(){
PMApplication thisApp = PMApplication.getInstance();
AppDelegate delegate = thisApp.getAppDelegate();
final Context context = this;
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
// Let's declare our receiver
registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
Log.d("", "Lets register for Push");
GCMRegistrar.register(this, SENDER_ID);
}else {
if(GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
String apnsToken = delegate.sso.getAPNSToken();
if(!apnsToken.equals(regId)){
Log.d("", "The Device RegId has changed on GCM Servers");
// We should let our servers know about this
ServerUtilities.update(regId, context);
}
} else {
Log.d("","Is not register on PM Server");
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context, regId);
// At this point all attempts to register with the app
// server failed, so we need to unregister the device
// from GCM - the app will try to register again when
// it is restarted. Note that GCM will send an
// unregistered callback upon completion, but
// GCMIntentService.onUnregistered() will ignore it.
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
gcminentservice.getNotificationIntent(上下文)
。此行返回我想要开始的活动的意图
只要有通知,就会调用onReceive,但对话框仅显示我是否在主活动中。因此,如果应用程序处于不同的活动中,onReceive
仍会被调用,但对话框不会显示,因此我无法启动正确的活动
如何在BroadcastReceiver上显示有关当前可见活动的对话框?在玩这个对话框并在google上搜索时,我遇到了一个解决方案。这不是最好的,但很有效。我仍然不相信在安卓系统中没有一个简单的方法来获取当前的上下文。因此,无论当前活动是什么,我都会设法显示对话框:在我的singleton类(AppDelegate)上有一个类型为Context的公共静态属性,在每个活动上,我覆盖onResume方法并将上下文设置为当前活动,如AppDelegate.current_Context=this。然后在我的对话框中:AlertDialog.Builder(AppDelegate.CURRENT_上下文)
private final BroadcastReceiver mHandleMessageReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
Log.d("","BroadcastReceiver onReceive");
notificationIntent = GCMIntentService.getNotificationIntent(context);
new AlertDialog.Builder(context)
.setMessage(newMessage+". Would you like to see it right now?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Show update
startActivity(notificationIntent);
}
})
.setNegativeButton("No", null).show();
}
};