Wednesday, November 5, 2014

Android Listen to Network State Change


Instagram This will help you to detect network changing states in android phone. For get an idea abut the API use for this read Followings.

Following Steps will do it. After setup the code and run the app just change your network status to flight mode and then again revert.


First we need to add required permission to read the changes of network connectivity. Insert following line in you AndroidManifest.xml file to do that.



       <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
       <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Now you good to go with the creating of your service.
Following is the code for the service that will detect the changes of network. (You can start  this even as BroadcastReceiver) 

Get an idea about Services Vs BroadcastReceivers in android. 





package com.testState.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class NetworkChangeService extends Service {

    String phonestate;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        Log.d("NetworkChangeService", "Starting Network Change Status Service");
        getServiceStateChange();
    }

    /**
     * Catch PhoneServiceState Changing
     */
    private void getServiceStateChange() {
        TelephonyManager telMng = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telMng.listen(new PhoneStateListener() {

            @Override
            public void onServiceStateChanged(ServiceState serviceState) {
                super.onServiceStateChanged(serviceState);

                switch (serviceState.getState()) {
                    case ServiceState.STATE_EMERGENCY_ONLY:
                        Log.d("NetworkChangeService", "STATE_EMERGENCY_ONLY");
                        phonestate = "STATE_EMERGENCY_ONLY";
                        Toast.makeText(this, "State : " + phonestate, Toast.LENGTH_SHORT).show();
                    break;
                case ServiceState.STATE_IN_SERVICE:
                    Log.d("NetworkChangeService", "STATE_IN_SERVICE");
                    phonestate = "STATE_IN_SERVICE";
                    Toast.makeText(this, "State : " + phonestate,
                            Toast.LENGTH_SHORT).show();
                    break;
                case ServiceState.STATE_OUT_OF_SERVICE:
                    Log.d("NetworkChangeService", "STATE_OUT_OF_SERVICE");
                    phonestate = "STATE_OUT_OF_SERVICE";
                    Toast.makeText(this, "State : " + phonestate,
                            Toast.LENGTH_SHORT).show();
                    break;
                case ServiceState.STATE_POWER_OFF:
                    Log.d("NetworkChangeService", "STATE_POWER_OFF");
                    phonestate = "STATE_POWER_OFF";
                    Toast.makeText(this, "State : " + phonestate,
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Log.d("NetworkChangeService", "Unknown");
                    phonestate = "Unknown";
                    Toast.makeText(this, "State : " + phonestate,
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, PhoneStateListener.LISTEN_SERVICE_STATE);
    }
}

After that start the service from your preferred activity.

package com.testState.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Activity", "Came in activity");
        // From this line service will start
        startService(new Intent(this, NetworkChangeService.class));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}



Don't forget to define the service in your AndroidManifest.xml file. Otherwise the service won't start.
<service
       android:name="com.testState.service.NetworkChangeService" 
       android:enabled="true" >
</service>
Happy Hacking!!!

1 comment:

  1. How to Play Baccarat (with Pictures) | The Wire
    You'll love to play a 온카지노 baccarat game. It's like having the dealer in 바카라 the background play the game of cards, and the febcasino dealer looks at

    ReplyDelete