我的MainActivity中有“check_wifi_available”方法,它检测以“SDF-”字符开头的特定SSID。该方法在那里工作得很好,但我有问题,当我超出范围我仍然得到最后的扫描结果虽然我超出范围所以我欺骗从我的“ConnectionBroadcast”类的呼叫,但在短时间后调用它应用程序崩溃。
错误:
<?php
04-08 00:13:25.412: E/AndroidRun04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2567)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.access$1800(ActivityThread.java:161)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.os.Looper.loop(Looper.java:157)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-08 00:17:48.229: E/AndroidRuntime(849): at dalvik.system.NativeStart.main(Native Method)
04-08 00:17:48.229: E/AndroidRuntime(849): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.Activity.getSystemService(Activity.java:4713)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.MainActivity.check_wifi_available(MainActivity.java:82)
04-08 00:17:48.229: E/AndroidRuntime(849): at com.bustracker.ConnectionBroadcast.onReceive(ConnectionBroadcast.java:44)
04-08 00:17:48.229: E/AndroidRuntime(849): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
04-08 00:17:48.229: E/AndroidRuntime(849): ... 10 more
?>
package com.bustracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
public class ConnectionBroadcast extends BroadcastReceiver {
MainActivity wifi;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
boolean isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"Please connect the device to the internet.",
Toast.LENGTH_SHORT).show();
}
} else if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
wifi = new MainActivity();
wifi.check_wifi_available();
}
}
}
<receiver android:name=".ConnectionBroadcast" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.net.wifi.SCAN_RESULTS" />
</intent-filter>
</receiver>
答案 0 :(得分:1)
广播接收器内的此代码不正确。您无法实例化活动对象。
wifi = new MainActivity();
wifi.check_wifi_available();
你需要把onReceive中的任何代码放在check_wifi_available()里面。