繁体   English   中英

如何从MainActivity外部的BroadcastReceiver类重新加载/刷新Main Activity-Android

[英]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.

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