在 MainActivity 中调用方法和变量 class
call method and Variable in the MainActivity class
我想将内部 BroadcastReceiverListener 放在 MainActivity 中,它在那里工作得很好,在 serperate class 中。我不知道如何从 MainActivity 调用方法 "deliverBestAccessPoint(updatedResults)" 并从 BroadcastReceiverListener 在 MainActivity 中设置变量 "textWifi" 的问题。我该如何管理?我希望有人能用代码回答以理解他的答案。
感谢任何帮助。
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)) {
}
}
一种方法是将 Intent 从您的广播接收器发送到 activity:
编辑
最后应该是这样的。但我没有具体说明如何将自定义对象列表转换为 JSON 数组并返回,这是一个完全独立的问题,有其自身的潜在困难。您可以弄清楚如何使用标准转换器或使用某些库来完成此操作。我个人更喜欢 GSON, it 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.");
}
}
}
另一种方法是将 MainActivity
添加为 BroadcastReceiver
的 WeakReference
成员:
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)) {
}
}
并且在 MainActivity 中注册时:registerReceiver(new BroadcastReceiverListener (this), mIntentFilter);
我想将内部 BroadcastReceiverListener 放在 MainActivity 中,它在那里工作得很好,在 serperate class 中。我不知道如何从 MainActivity 调用方法 "deliverBestAccessPoint(updatedResults)" 并从 BroadcastReceiverListener 在 MainActivity 中设置变量 "textWifi" 的问题。我该如何管理?我希望有人能用代码回答以理解他的答案。
感谢任何帮助。
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)) {
}
}
一种方法是将 Intent 从您的广播接收器发送到 activity:
编辑
最后应该是这样的。但我没有具体说明如何将自定义对象列表转换为 JSON 数组并返回,这是一个完全独立的问题,有其自身的潜在困难。您可以弄清楚如何使用标准转换器或使用某些库来完成此操作。我个人更喜欢 GSON, it 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.");
}
}
}
另一种方法是将 MainActivity
添加为 BroadcastReceiver
的 WeakReference
成员:
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)) {
}
}
并且在 MainActivity 中注册时:registerReceiver(new BroadcastReceiverListener (this), mIntentFilter);