简体   繁体   中英

How to reload/refresh Main Activity from BroadcastReceiver class outside MainActivity - Android

I have a MainActivity class that calls a server to fetch messages in the onCreate() method.

Then I have a BroadcastReceiver class outside the MainActivity class.

This is my BroadcastReceiver class:

  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 

    }

}

}

If extras != null I want to refresh my MainActivity class so that it does the onCreate() call again. How can I do this?

This is my manifest file snippet:

  <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实例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM