gpt4 book ai didi

java - 如何从广播接收器向主要 Activity 发送消息

转载 作者:行者123 更新时间:2023-11-30 11:28:18 27 4
gpt4 key购买 nike

我知道这是一个基本问题,这里有很多类似的问题,但是,我浏览了几十个,他们都以特定的方式提出问题,他们的回答并不能解决我的问题。

在我的主要 Activity 类中,我有:

public static class GcmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
}
}

当我收到某个 gcm 消息时,我想转换到一个新的屏幕/Activity 。这需要在 mainActivity 的上下文中完成。那么我如何向主要 Activity 发送消息以告诉它执行此操作。我想我应该使用处理程序,但在这种情况下我不知道确切的语法是什么。我从不“创建”广播接收器,所以我不能在其构造函数中传入一些处理程序。 BCR 是通过我的 list 文件的 Intent 过滤器设置的。 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

}

}

然后广播接收器在您的主 Activity 中调用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com