Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
.
.
>
<receiver android:name=".Receivers.NetworkConnectivityChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
.
.
/>
Process: org.fossasia.openevent, PID: 4413
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in org.fossasia.openevent.Receivers.NetworkConnectivityChangeReceiver@8a12e8b
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
at android.support.v7.app.AlertController.installContent(AlertController.java:213)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
at android.app.Dialog.show(Dialog.java:295)
at org.fossasia.openevent.Receivers.NetworkConnectivityChangeReceiver.onReceive(NetworkConnectivityChangeReceiver.java:50)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881)
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
package org.fossasia.openevent.Receivers;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import org.fossasia.openevent.OpenEventApp;
import org.fossasia.openevent.R;
import org.fossasia.openevent.events.ConnectionCheckEvent;
import org.fossasia.openevent.widget.DialogFactory;
import timber.log.Timber;
/**
* User: Harshit Dwivedi
* Date: 02/03/16
* <p/>
* Network connectivity change receiver to detect changes in network
*/
public class NetworkConnectivityChangeReceiver extends BroadcastReceiver {
public boolean isConnected = false;
@Override
public void onReceive(final Context context, final Intent intent) {
Timber.i("Network information received %s", intent);
ConnectionCheckEvent event = new ConnectionCheckEvent(isConnected);
Dialog d = DialogFactory.createSimpleActionDialog(context,
R.string.net_unavailable,
R.string.turn_on,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent setNetworkIntent = new Intent(Settings.ACTION_SETTINGS);
context.startActivity(setNetworkIntent);
}
});
if (isNetworkAvailable(context)) {
if (!event.connState()) {
event.isConnected = true;
d.hide();
}
} else {
event.isConnected = false;
d.show();
}
Timber.i("Network connected %s", event.isConnected);
OpenEventApp.postEventOnUIThread(event);
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.