Learn how to use system services like ConnectivityManager, WifiManager, AudioManager etc.

Example 1: System Service Examples

In this example you will learn how to:

  1. Check if network is connected
  2. Check if wifi is enabled, if not then enable it.
  3. Get system audio volume.
  4. Get package name.

Step 1: Create Project

The first step is to create a Android Project in Android Studio.

Step 2: Add Dependencies

No external dependencies are needed.

Step 3: Design Layouts

Create your MainActivity layout as follows:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:id="@+id/network"
        android : text = " Check if there is a network "
        android:onClick="doClick"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:id="@+id/wifi_manager"
        android : text = " Turn off wifi "
        android:onClick="doClick"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:id="@+id/getvoice"
        android : text = " Get system volume "
        android:onClick="doClick"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:id="@+id/getpackagename"
        android : text = " Get the current process package name "
        android:onClick="doClick"
        android:layout_height="wrap_content" />
</LinearLayout>

Step 4: Write Code

Here’s the full code:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super . onCreate (savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.main,null);
        setContentView(view);

    }

    public void doClick(View view){
        switch (view.getId()){
            case R.id.network:
                if (isNetWorkConnected(MainActivity.this)==true){
                    Toast . MakeText( MainActivity . This , "The network is open " , Toast . LENGTH_SHORT ) . Show();
                }else {
                    Toast . MakeText( MainActivity . This , "The network is not connected " , Toast . LENGTH_SHORT ) . Show();
                }
                break;
            case R.id.wifi_manager:
                WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(WIFI_SERVICE);
                if (wifiManager.isWifiEnabled()){
                    wifiManager.setWifiEnabled(false);
                    Toast . MakeText( MainActivity . This , " wifi is closed " , Toast . LENGTH_SHORT ) . Show();
                }else {
                    wifiManager.setWifiEnabled(true);
                    Toast . MakeText( MainActivity . This , " wifi has been turned on " , Toast . LENGTH_SHORT ) . Show();
                }
                break;
            case R.id.getvoice:
                AudioManager audioManager = (AudioManager) MainActivity.this.getSystemService(AUDIO_SERVICE);
                int max =      audioManager . getStreamMaxVolume( AudioManager . STREAM_SYSTEM ); // Maximum system volume value
                int current =      audioManager . getStreamVolume( AudioManager . STREAM_RING ); // system maximum volume value
                Toast . MakeText( MainActivity . This , " The maximum volume of the system is: " + max + " , and the current volume is: " + current, Toast . LENGTH_SHORT ) . Show();
                break;
            case R.id.getpackagename:
                String packagename =  MainActivity.this.getPackageName();

                Toast.makeText(MainActivity.this,"The package name is :"+packagename,Toast.LENGTH_SHORT).show();
                break;
        }
    }
    public boolean isNetWorkConnected(Context context){
        if (context != null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); //context.getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo mNetWorkInfo =  mConnectivityManager.getActiveNetworkInfo();
            if (mNetWorkInfo != null){
                return mNetWorkInfo.isAvailable();
            }
        }
        return false;
    }
}

Step 5: Add Permissions

Add the following permissions in your AndroidManifest.xml:

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

Run

Copy the code into your project or download the code in the reference links below.

Reference

Find the reference links below:

Number Link
1. Download code
2. Follow code author