public class Information extends BroadcastReceiver{
public static String ChangeDate;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
Toast.makeText(context, "Internet Available", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Internet not Available", Toast.LENGTH_SHORT).show();
RahuActivity.nextAlaram();
}
}
}
清单.xml
<receiver android:name="com.astrobix.service.Information" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
当我在我的手机上运行此代码时,请任何人帮助我,它显示了两种 Toast 方法。当我第一次关闭 Wi-Fi 时,它会显示 Internet 可用消息,然后再次显示 Internet 不可用消息。它必须只显示一个消息,我认为这就是为什么我的RahuActivity.nextAlaram()
显示异常。
Logcat here :-
08-02 16:25:54.309: E/AndroidRuntime(21170): java.lang.RuntimeException: Unable to start receiver com.astrobix.service.Information: java.lang.NullPointerException