相关疑难解决方法(0)

在Android应用程序中检查互联网连接的广播接收器

我正在开发一个用于检查互联网连接的Android广播接收器.

问题是我的广播接收器被调用了两次.我希望它只在网络可用时才被调用.如果它不可用,我不希望得到通知.

这是广播接收器

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            // Do something

            Log.d("Network Available ", "Flag No 1");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是manifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" …
Run Code Online (Sandbox Code Playgroud)

java android broadcastreceiver android-networking android-wifi

228
推荐指数
12
解决办法
25万
查看次数

如果应用程序位于前台,Toast如何才能显示?

我有一个Toast从内部调用的Service.无论设计的状态如何,它总是会出现.

如何让它只在我的应用程序位于前台时显示,而不是在其他应用程序位于前面时?也就是说,无需将其移动到Activity代码中.

android

5
推荐指数
1
解决办法
2777
查看次数