Android 与广播接收器问题同步活动
我想将我的活动与谷歌云消息同步。 当GCM消息接收到自己的接收者时,获取消息并创建通知,然后将我的自定义消息广播给活动接收者 另一方面,我的活动有自己的动态注册的BroadcastReceiver,它接收我的cusom消息 现在情况是这样的:Android 与广播接收器问题同步活动,android,android-intent,synchronization,broadcastreceiver,google-cloud-messaging,Android,Android Intent,Synchronization,Broadcastreceiver,Google Cloud Messaging,我想将我的活动与谷歌云消息同步。 当GCM消息接收到自己的接收者时,获取消息并创建通知,然后将我的自定义消息广播给活动接收者 另一方面,我的活动有自己的动态注册的BroadcastReceiver,它接收我的cusom消息 现在情况是这样的: 当应用程序打开时,不点击通知,我的活动 接收器接收消息并显示 但当活动在单击通知后关闭时 接收并打开应用程序 我试过这样的方法: 在maifest上注册一个类广播接收器。但我无法将其与我的活动同步。因为我发现外部接收器可以仅通过putextra使用ac
- 当应用程序打开时,不点击通知,我的活动 接收器接收消息并显示
- 但当活动在单击通知后关闭时 接收并打开应用程序
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG", "unreciver");
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
//Showing received message
//lblMessage.append(newMessage + "\n");
Log.i("LOG", "unreciver messsage:"+newMessage);
//Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
loadReciverDialog(newMessage);
// Releasing wake lock
WakeLocker.release();
}
};
这是从GCM接收消息并创建通知的服务部分:
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
Log.i("LOG", "GCM service Message "+message);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) {
Log.i("LOG", "genetaret notify");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
此部分显示消息:
static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.i("LOG", "commonutils msg="+message);
context.sendBroadcast(intent);
}
我最终用这种方式解决了这个问题:
- 我保留了活动中的广播接收器,并创建了一个功能,用于检查是否存在额外的内容。OnReceive功能下的代码有效
- 在创建通知后,将消息放入额外的主活动以获取它们