【发布时间】:2017-05-15 14:43:45
【问题描述】:
我陷入了一个问题。我有一个广播接收器,它调用类的方法,在构造函数中获取上下文和主要活动引用。我不知道如何访问广播接收器中的主要活动。这是我的代码:
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
if (context == null) {
throw new IllegalArgumentException();
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (info.isConnected()) {
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
Class myclass = new Class(context,mainactivity reference); //dont know how to get main activity here
}
}
}
}
有没有什么方法可以在没有 Intent 的情况下获得它,或者有其他方法。我处于学习阶段,任何帮助将不胜感激。
在清单中:
<receiver android:name="myreceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
【问题讨论】:
标签: android android-activity broadcastreceiver main-activity