![](/img/trans.png)
[英]how do I get value from BroadcastReceiver activity to main activity
[英]How to send message to main activity from broadcastreceiver
我知道這是一個基本問題,但是這里有很多類似的問題,但是,我瀏覽了數十個問題,他們都以特定的方式提出問題,而他們的回答並不能解決我的問題。
在我的主要活動課中,我有:
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();
}
}
};
我希望它對您有用。
我認為收到GCM消息后,您想切換到某些活動/屏幕。 為此,下面是從BroadcastReceiver
啟動活動的代碼:
公共靜態類GcmBroadcastReceiver擴展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
//syntax: i.setClassName("packageName","Activity to start inside packageName:);
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
處理程序是您正在啟動的工作線程與主線程之間的一種通信方式。
由於您只是開始一項活動,因此不需要處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.