我知道这是一个基本问题,但是这里有很多类似的问题,但是,我浏览了数十个问题,他们都以特定的方式提出问题,而他们的回答并不能解决我的问题。
在我的主要活动课中,我有:
public static class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
当我收到某些gcm消息时,我想过渡到新的屏幕/活动。这需要从mainActivity的上下文中完成。因此,我如何向主要活动发送消息以告诉它执行此操作。我认为我应该使用处理程序,但是在这种情况下,我不知道确切的语法是什么。我从不“创建”广播接收器,因此无法在其构造函数中传递某些处理程序。 BCR是通过清单文件中的意图过滤器设置的。这就是gcm上的android教程的设置方式,因此我宁愿不要动态创建广播接收器(除非这是唯一的方法)。
最佳答案
public class GCMIntentService extends GCMBaseIntentService {
public static final String PROJECT_ID = "345657565857";
private static final String TAG = "GCMIntentService";
ModelNotificationMessage modelNotificationMessage;
public GCMIntentService() {
super(PROJECT_ID);
Log.d(TAG, "GCMIntentService init");
}
@Override
protected void onError(Context ctx, String sError) {
// TODO Auto-generated method stub
Log.d(TAG, "Error: " + sError);
}
@Override
protected void onMessage(Context ctx, Intent intent) {
Log.d(TAG, "Message Received");
String message = intent.getStringExtra("message");
Log.d(TAG, "Message Received" + message);
sendNotification(message);
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("GCM_RECEIVED_ACTION");
broadcastIntent.putExtra("gcm", message);
ctx.sendBroadcast(broadcastIntent);
}
private void sendNotification(String message) {
// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification;
CharSequence tickerText = message; // ticker-text
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = modelNotificationMessage.getKey();
CharSequence contentText = message;
Intent notificationIntent = null;
int NOTIFICATION_ID = 9999;
NOTIFICATION_ID = CommonVariable.notification_message;
notificationIntent = new Intent(this, ViewMessages.class);
// and this
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
// Play default notification sound
notification.defaults |= Notification.DEFAULT_ALL;
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
@Override
protected void onRegistered(Context ctx, String regId) {
// TODO Auto-generated method stub
// send regId to your server
Log.d(TAG, regId);
}
@Override
protected void onUnregistered(Context ctx, String regId) {
// TODO Auto-generated method stub
// send notification to your server to remove that regId
}
}
然后广播接收机会在您的mainactivity中调用onresume方法。
public void onResume() {
gcmFilter = new IntentFilter();
gcmFilter.addAction("GCM_RECEIVED_ACTION");
viewMessages.registerReceiver(gcmReceiver, gcmFilter);
}
// A BroadcastReceiver must override the onReceive() event.
private BroadcastReceiver gcmReceiver = new BroadcastReceiver() {
private String broadcastMessage;
@Override
public void onReceive(Context context, Intent intent) {
broadcastMessage = intent.getExtras().getString("gcm");
if (broadcastMessage != null && viewMessages != null) {
// display our received message
onResume();
}
}
};
我希望它对您有用。
关于java - 如何从广播接收器向主要 Activity 发送消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18928493/