简体   繁体   English

如何在没有任何活动的情况下在 Android 中启动 BraodCastReceiver?

[英]How to Launch BraodCastReceiver in Android without any Activity?

I want to show Toast Message when I turn on my inbuilt device Wi-Fi, not by any button in Activity.我想在打开内置设备 Wi-Fi 时显示 Toast 消息,而不是通过活动中的任何按钮。 My goal is to make an application which doesn't create any launcher but work in background.我的目标是制作一个不创建任何启动器但在后台工作的应用程序。 I tried but still there is no any Toast.我试过了,但仍然没有任何吐司。 My Receiver class is here:我的接收器类在这里:

public class Receiver extends BroadcastReceiver {

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

        String status = NetworkUtil.getConnectivityStatusString(context);

        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    }
}

and my NetworkUtil class is here:我的 NetworkUtil 类在这里:

public class NetworkUtil {

    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;


    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        } 
        return TYPE_NOT_CONNECTED;
    }

    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }

}

and my Manifest.xml is like here:我的 Manifest.xml 就像这里:

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


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="net.xyz.network.Receiver"
            android:label="Receiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

What mistake I am making here which is causing this to not output?我在这里犯了什么错误导致它不输出?

尝试

Toast.makeText(context.getApplicationContext(), status, Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM