【问题标题】:How to reload/refresh Main Activity from BroadcastReceiver class outside MainActivity - Android如何从 MainActivity 之外的 BroadcastReceiver 类重新加载/刷新 Main Activity - Android
【发布时间】:2014-03-27 12:16:41
【问题描述】:

我有一个 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()。我该怎么做?

这是我的清单文件 sn-p:

  <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
  • 使用第二个广播接收器或本地广播。

标签: java android android-activity broadcastreceiver


【解决方案1】:

使用意图启动MainActivity,我会将 android:launchMode 属性更改为“singleTop”,这样您就没有 2 个 MainActivity 实例

【讨论】:

  • 如何从位于 MainActivity 类之外的 BroadcastReceiver 类调用它?
  • madhu 的回答可能很好。我会将 android:launchMode 属性更改为“singleTop”,这样您就没有 2 个 MainActivity 实例
  • 非常感谢!将其添加为答案或编辑以上内容,我会接受吗?
猜你喜欢
  • 2017-04-12
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
相关资源
最近更新 更多