Wednesday 18 September 2013

On Launch
Enable/Disable Mobile Data-GPRS

on Mobile Data Enable
Enable/Disable Mobile Data-GPRS

on Mobile Data Disable

Enable/Disable Mobile Data-GPRS


Create new Android Project
Project Name: System Settings
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: SystemSettings
Package Name: com.shaikhhamadali.blogspot.systemsettings
Create layout file: activity_mobile_data

  1. create layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/TVMobileData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Mobile Data: Disable"
        android:textColor="#1BD6E0"
        android:textSize="40sp" />

    <ToggleButton
        android:id="@+id/tBMobileData"
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:textOff="Enable"
        android:textOn="Disable" />

</RelativeLayout>
  • Add Permission in Manifest:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


2. code of activity:
package com.shaikhhamadali.blogspot.systemsettings;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MobileData extends Activity{
 // constants
 static final String STATUS_ON = "Mobile Data: Enable";
 static final String STATUS_OFF = "Mobile Data: Disable";

 static final String TURN_ON = "Enable";
 static final String TURN_OFF = "Disable";

 // controls
 TextView TVMobileData;
 ToggleButton tBMobileData;



 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_mobile_data);
  // load controls
  TVMobileData=(TextView)findViewById(R.id.TVMobileData);
  tBMobileData=(ToggleButton)findViewById(R.id.tBMobileData);

  // set click event for button
  tBMobileData.setOnClickListener(new OnClickListener() {                     
   @Override
   public void onClick(View v) {
    // check current state first
    boolean state = isMobileDataEnable();
    // toggle the state
    if(state)toggleMobileDataConnection(false);
    else toggleMobileDataConnection(true);
    // update UI to new state
    updateUI(!state);  
   }
  });
 }
 public void updateUI(boolean state) {
  //set text according to state
  if(state) {
   TVMobileData.setText(STATUS_ON);
   tBMobileData.setText(TURN_OFF);             
  } else {
   TVMobileData.setText(STATUS_OFF);
   tBMobileData.setText(TURN_ON);
  }
 }
 public boolean isMobileDataEnable() {
  boolean mobileDataEnabled = false; // Assume disabled
  ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  try {
   Class cmClass = Class.forName(cm.getClass().getName());
   Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
   method.setAccessible(true); // Make the method callable
   // get the setting for "mobile data"
   mobileDataEnabled = (Boolean)method.invoke(cm);
  } catch (Exception e) {
   // Some problem accessible private API and do whatever error handling you want here
  }
  return mobileDataEnabled;
 }
 public boolean toggleMobileDataConnection(boolean ON)
 {
  try {
   //create instance of connectivity manager and get system connectivity service
   final ConnectivityManager conman = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
   //create instance of class and get name of connectivity manager system service class
   final Class conmanClass  = Class.forName(conman.getClass().getName());
   //create instance of field and get mService Declared field 
   final Field iConnectivityManagerField= conmanClass.getDeclaredField("mService");
   //Attempt to set the value of the accessible flag to true
   iConnectivityManagerField.setAccessible(true);
   //create instance of object and get the value of field conman
   final Object iConnectivityManager = iConnectivityManagerField.get(conman);
   //create instance of class and get the name of iConnectivityManager field
   final Class iConnectivityManagerClass=  Class.forName(iConnectivityManager.getClass().getName());
   //create instance of method and get declared method and type
   final Method setMobileDataEnabledMethod= iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
   //Attempt to set the value of the accessible flag to true
   setMobileDataEnabledMethod.setAccessible(true);
   //dynamically invoke the iConnectivityManager object according to your need (true/false)
   setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
  } catch (Exception e){
  }
  return true;
 }
}

3. note that:


4. conclusion:

  • Some information about how to toggle Mobile Data (Enable /Disable).
  • Know how to update UI after toggle.
  • know what is connectivity manager.

5. about the post:


  • The code seems to explain itself due to comments, if you have any question you can ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:
        you can download the source code here


Cheers,

Hamad Ali Shaikh