我已经分离了BroadcastReceiverListener并且我希望将“List ScanResult updatedResults = new ArrayList ScanResult”从具有Intent的seprate“BroadcastReceiverListener”类中的onReceive()传递给MainActivity。如何使用putExtra执行此操作?
我感谢任何帮助。
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)) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(context, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
// if (wifi.getWifiState() !=
// WifiManager.WIFI_STATE_ENABLING) {
// wifi.setWifiEnabled(true);
// }
}
WifiManager wifi = (WifiManager) context
.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 updateIntenet = new Intent(context, MainActivity.class);
updateIntenet.putParcelableArrayListExtra("broadcast_event",
updatedResults);
context.startActivity(intent);
}
}
}
}
MainActivity中的onNewIntent方法:
@Override
protected void onNewIntent(Intent intent) {
String intentResult = intent.getStringExtra("broadcast_event");
if (intentResult != null) {
//here I am getting the error: The method getParcelableArrayList(String) is undefined for the type MainActivity
List<ScanResult> results = getParcelableArrayList(intentResult);
String a = deliverBestAccessPoint(results);
textWifi.setText(a.toString());
} else {
textWifi.setText("No route is available.");
}
}
答案 0 :(得分:0)
由于ScanResult实现了Parcelable,您只需要更改:
// list of access points from the last scan
Lst<ScanResult> updatedResults = new ArrayList<ScanResult>();
要:
// list of access points from the last scan
ArrayList<ScanResult> updatedResults = new ArrayList<ScanResult>();