我想通过使用Parcelable接口将数据从我的BroadcastReceiverListener类中的onReceive()
传递到MainActivity中的onNewIntent()
方法。我怎么能让它工作?如何在onNewIntent()
?
我感谢任何帮助。
BroadcastReceiverListener类:
public class BroadcastReceiverListener extends BroadcastReceiver implements
Parcelable {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
ArrayList<ScanResult> updatedResults = new ArrayList<ScanResult>();
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
}
}
Intent updateIntenet = new Intent(context, MainActivity.class);
updateIntenet.putParcelableArrayListExtra("broadcast_event",
updatedResults);
context.startActivity(updateIntenet);
}
}
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
}
}
此方法位于MainActiity:
@Override
protected void onNewIntent(Intent intent) {
String intentResult = intent.getStringExtra("broadcast_event");
if (intentResult != null) {
List<ScanResult> results = getParcelableArrayList(intentResult);
String a = deliverBestAccessPoint(results);
textWifi.setText(a.toString());
} else {
textWifi.setText("No route is available.");
}
}
答案 0 :(得分:1)
你应该对你收到的意图调用getParcelableArrayListExtra:
List<ScanResult> results = intent.getParcelableArrayListExtra("broadcast_event");