1
I'm trying to implement a scenario, if my application loses internet connectivity I'm posting a alert dialog to the user.
I'm planning to use Broadcast receiver to check internet connectivity in my app by using register and unregister in my main activity.
Here is the code:
**BroadcastReceiver** class : here I'm checking the internet connectivity.
Wi-Fi 또는 데이터 연결 서비스를 확인하고 있으며 사용할 수없는 경우 사용자에게 경고합니다.창을 추가 할 수 없습니다. - 토큰 null은 방송 수신자의 OnReceive 안에있는 애플리케이션에 해당하지 않습니다.
public class NetworkChangeReceiver extends BroadcastReceiver {
private static final String TAG = "ConnectionReceiver";
private AlertDialog alertDialog;
public NetworkChangeReceiver() {
}
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if (isConnected) {
try {
if (alertDialog != null && alertDialog.isShowing())
alertDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (alertDialog == null || !alertDialog.isShowing()) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("No internet connection");
builder.setMessage("check your connection.");
builder.setPositiveButton(context.getString(R.string.confirm_button_text), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog = builder.create();
alertDialog.show();
}
}
}
}
}
**And in my Activity I'm registering the broadcast receiver.** :
public class MyActivity extends Activity {
private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver();
@Override
protected void onResume() {
super.onResume();
registerReceiver(networkChangeReceiver, new IntentFilter(
"android.net.conn.CONNECTIVITY_CHANGE"));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(networkChangeReceiver);
}
}
When I put my phone in airplane mode , alert dialog pops up and after 2 seconds my app gives an "Unable to add window -- token null is not for an application " exception. and this exception occurs 2 times.
**here is my log**:
AndroidRuntime: FATAL EXCEPTION: main Process: , PID: 9923 java.lang.RuntimeException: Unable to start receiver xxxx.NetworkChangeReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
My **Manifest** file:
<application
android:name="xxxxx"
android:allowBackup="true"
tools:replace = "android:icon"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<receiver android:name=".xxx.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="xxxx"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
누군가가 예외를 해결하는 방법을 알려주고 예외가 두 번 발생하는 이유는 무엇입니까?
[BroadcastReceiver 클래스에서 경고 대화 상자를 생성하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/7229951/how-to-raise-an-alert-dialog-from-broadcastreceiver-class) –
Receiver를 두 곳에서 등록했기 때문에 두 번이나 진행됩니다. 매니 페스트 및 '활동' –
내 활동에는 20 개의 다른 조각이 있습니다. 사용자가 다른 조각으로 이동할 때마다 경고 대화 상자를 표시하려고합니다. 하지만 onResume이 한 번 호출 될 때만 표시됩니다. 내가 다른 경고 메시지를 다시 표시하고 싶을 때 경고 대화 상자를 닫은 후 다시 끕니다. 어떤 생각을하는 방법? 모든 파편에 등록해야합니까? – DroidDev