0

我想在我的应用程序中检查我们是否已连接到 Internet。我正在使用广播接收器。如果连接丢失,我想更改我的数据库 SQLite 中的条目并停止每分钟在另一个线程中执行 htpp 请求的服务。Log.i 不显示任何内容,当我尝试更新我的数据库或停止服务时,我的应用程序崩溃并且我在 logcat 中没有错误消息,也没有异常。

public class InternetConnection extends BroadcastReceiver {

private AccountBDD accountBdd;

@Override
public void onReceive(Context context, Intent intent) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        State networkState = networkInfo.getState();
        if (networkState.compareTo(State.CONNECTED) == 0)
            Log.i("INTERNET CONNEC", "connection retrieved");
        Toast.makeText(context, "RETRIEVED CONNECTION ", Toast.LENGTH_LONG)
                .show();
    }

    Toast.makeText(context, "CONNECTION LOST", Toast.LENGTH_LONG).show();
    Log.i("INTERNET CONNEC", "loss of connection");

    // context.stopService(new Intent(context, PeriodicSync.class));
    // accountBdd.updateSyncUtility(syncUtility);
    // Log.i(" Value ", " offlineWork = "
    // + accountBdd.getSyncUtility().getOfflineWork());
}
} 

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="13" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


<application
    android:label="@string/app_banner" 
    android:name="SQLiteApplication">

    <service android:name="PeriodicSync" /> 

    <receiver android:enabled="true" android:name="com.android.InternetConnection">
     <intent-filter>
         <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
     </intent-filter>
 </receiver>


</application>

有人有想法吗?谢谢

4

0 回答 0