[英]How to reload/refresh Main Activity from BroadcastReceiver class outside MainActivity - Android
我有一個MainActivity類,該類調用服務器以在onCreate()方法中獲取消息。
然后我在MainActivity類之外有一個BroadcastReceiver類。
這是我的BroadcastReceiver類:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
Bundle extras = intent.getExtras();
if (extras != null) {
// This is where I want to refresh/reload MainActivity
}
}
}
如果Extras!= null,我想刷新MainActivity類,以便再次執行onCreate()調用。 我怎樣才能做到這一點?
這是我的清單文件片段:
<receiver
android:name="za.co.vine.samplepush.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="za.co.vine.samplepush" />
</intent-filter>
</receiver>
使用意圖啟動MainActivity
然后將android:launchMode屬性更改為“ singleTop”,這樣您就不會有2個MainActivity實例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.