You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Studio开发:实现状态栏WiFi/GPRS等图标显示与隐藏控制

Hey there! I’ve tackled similar status bar icon control requirements before, so let’s walk through how to make this work in your Android Studio project, plus answer your question about other icons.

Status Bar Icon Control (WiFi/GPRS + More)

First, a critical note upfront: regular third-party apps don’t have native access to directly show/hide system status bar icons—Android’s security model restricts this kind of system-level UI manipulation. But there are two viable paths depending on your app’s use case:

1. For Custom ROM/System Apps (Requires System Signature)

If your app is preloaded on a custom ROM or has system-level privileges, you can use hidden system APIs via reflection to directly control icon visibility without altering the actual feature state:

WiFi Icon Control

// Hide WiFi icon
private void hideWifiIcon() {
    try {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        Method toggleIconMethod = wifiManager.getClass().getMethod("setWifiVisibility", int.class);
        toggleIconMethod.invoke(wifiManager, 0); // 0 = hidden, 1 = visible
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
}

// Show WiFi icon
private void showWifiIcon() {
    try {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        Method toggleIconMethod = wifiManager.getClass().getMethod("setWifiVisibility", int.class);
        toggleIconMethod.invoke(wifiManager, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

GPRS (Mobile Data) Icon Control

Similar logic applies here using ConnectivityManager hidden APIs:

// Hide GPRS icon
private void hideGprsIcon() {
    try {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        Method setMobileDataVisibility = connManager.getClass().getMethod("setMobileDataVisibility", int.class);
        setMobileDataVisibility.invoke(connManager, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// Show GPRS icon
private void showGprsIcon() {
    try {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        Method setMobileDataVisibility = connManager.getClass().getMethod("setMobileDataVisibility", int.class);
        setMobileDataVisibility.invoke(connManager, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Note: These APIs are @hide in the Android SDK, so they’re not officially supported and may vary across ROMs. Your app must be signed with the system’s platform key to use them.

2. For Regular Third-Party Apps (No System Privileges)

Without system access, you can only indirectly control icon visibility by modifying the underlying feature state (e.g., turn WiFi off to make its icon disappear):

WiFi Icon Toggle

First, add the required permission to AndroidManifest.xml:

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

Then implement the button logic:

Button btnHideWifi = findViewById(R.id.button1);
Button btnShowWifi = findViewById(R.id.button1_1);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

btnHideWifi.setOnClickListener(v -> {
    if (wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(false);
    }
});

btnShowWifi.setOnClickListener(v -> {
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
});

GPRS (Mobile Data) Icon Toggle

Add the necessary permission:

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

Implement the toggle logic (note differences for Android 6.0+):

Button btnHideGprs = findViewById(R.id.button2);
Button btnShowGprs = findViewById(R.id.button2_1);
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

// Hide GPRS icon (disable mobile data)
btnHideGprs.setOnClickListener(v -> {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        NetworkRequest cellularRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                .build();
        connManager.requestNetwork(cellularRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                connManager.unregisterNetworkCallback(this);
                connManager.bindProcessToNetwork(null);
            }
        });
    } else {
        // Pre-Lollipop: Use reflection
        try {
            Method setMobileDataEnabled = connManager.getClass().getMethod("setMobileDataEnabled", boolean.class);
            setMobileDataEnabled.invoke(connManager, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

// Show GPRS icon (enable mobile data)
btnShowGprs.setOnClickListener(v -> {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        NetworkRequest cellularRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                .build();
        connManager.requestNetwork(cellularRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                connManager.unregisterNetworkCallback(this);
                connManager.bindProcessToNetwork(network);
            }
        });
    } else {
        try {
            Method setMobileDataEnabled = connManager.getClass().getMethod("setMobileDataEnabled", boolean.class);
            setMobileDataEnabled.invoke(connManager, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

Can You Control Other Status Bar Icons?

Great question! Here’s the breakdown for other common icons:

  • Bluetooth, NFC, Location: Same as WiFi/GPRS—regular apps can only toggle the feature on/off to make the icon appear/disappear. System apps can use hidden APIs to control visibility without altering feature state.
  • Battery, Volume, Clock: These are core system UI elements. No app (even system-level) can easily hide them without heavily modifying the ROM’s system UI code, as they’re part of the status bar’s core layout.
  • Notification Icons: You can control your own app’s notification icons (by canceling/showing notifications), but you can’t hide notifications from other apps unless you have system-level access to manage notifications.

内容的提问来源于stack exchange,提问作者mani hani

火山引擎 最新活动