我想把内部的BroadcastReceiverListener放在MainActivity中,它在serperate类中运行良好。问题我不知道如何调用方法" deliverBestAccessPoint(updatedResults)"来自MainActivity并设置变量" textWifi"在BroadcastReceiverListener的MainActivity中。我该如何管理?我希望有人会用代码回答,以理解他的答案。
我感谢任何帮助。
private class BroadcastReceiverListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "SV-"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
// Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
// Matcher m = p.matcher(ssid);
// if(m.matches()){}else{}
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
} else {
textWifi.setText("No route is available.");
}
}
if (updatedResults.size() > 0) {
String a = deliverBestAccessPoint(updatedResults);
textWifi.setText(a.toString());
}
} else {
textWifi.setText("There is no WiFi in range.");
}
Intent updateIntent = new Intent(context, MainActivity.class);
updateIntent.putExtra("broadcast_event", updatedResults);
context.startActivity(intent);
}
}
else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
}
}
&#13;
答案 0 :(得分:1)
一种方法是将广播接收器中的意图发送到活动:
修改强>
最终应该是这样的。但是我没有指定如何将自定义对象列表转换为JSON数组,这是一个完全独立的问题,它有自己的潜在困难。你可以弄清楚如何使用标准转换器,或使用一些库。我个人更喜欢GSON,deals非常喜欢对象的集合而不需要你的努力。
public class BroadcastReceiverListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "SV-"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
// Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
// Matcher m = p.matcher(ssid);
// if(m.matches()){}else{}
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
}
}
Intent updateIntent = new Intent(context, MainActivity.class);
String extras = convertToJson(updatedResults);
updateIntent.putExtra("broadcast_event", extras);
context.startActivity(intent);
}
}
} else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
}
}
}
public class YourActivity extends Activity {
...
@Override
protected void onNewIntent(Intent intent) {
String resultsJson = intent.getStringExtra("broadcast_event");
if (resultsJson != null) {
List<ScanResult> results = convertFromJson(resultsJson);
String a = deliverBestAccessPoint(results);
textWifi.setText(a.toString());
} else {
textWifi.setText("No route is available.");
}
}
}
答案 1 :(得分:0)
另一种方法是将MainActivity
添加为WeakReference
的{{1}}成员:
BroadcastReceiver
在MainActivity中注册时:private class BroadcastReceiverListener extends BroadcastReceiver {
public BroadcastReceiverListener (MainActivity mainActivity){
mainActivityRef = new WeakReference<>(mainActivity);
}
private WeakReference<MainActivity> mainActivityRef;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
// This method"deliverBestAccessPoint" is in MainActivity class
MainACtivity mActivity = mainActivityRef.get();
if(mActivity != null){//could be collected by GC
String a = mActivity .deliverBestAccessPoint(updatedResults);
//I want to set the "textwifi" varaible in MainActivity
mActivity.textWifi.setText(a.toString());
}
}
}
else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
}
}