How to turn on 3G mobile data programmatically in Android?

user1782267 picture user1782267 · Nov 1, 2012 · Viewed 30.1k times · Source
package com.testing.connection;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ConnectionActivity extends Activity implements OnClickListener{

    Button press;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        press = (Button)findViewById(R.id.button1);
        press.setOnClickListener(this);
    }

    public void onClick(View view){
        ConnectivityManager mgr = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

        boolean is3G = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        boolean isWifi = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

        if(isWifi){
            Toast.makeText(this, "WiFi connected...", Toast.LENGTH_LONG).show();
            sendMail();
        }
        else{
            //**Turn on Mobile Data
            //**Then sendMail()
            //**Turn off Mobile Data
        }
    }

    public void sendMail() throws MessagingException{

        String host = "smtp.gmail.com";
        String password = "abc123";
        String from = "[email protected]";
        String toAddress = enterEmail.getText().toString();

        Properties properties = System.getProperties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtps.auth", true);
        properties.put("mail.smtp.starttls.enable", true);
        Session session = Session.getInstance(properties, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, toAddress);
        message.setSubject("Anti-Theft Attachment");

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("Your email address is saved as backup email in Anti-Theft Application");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);

        try{
            Transport transport = session.getTransport("smtps");
            transport.connect(host, from, password);
            transport.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            transport.close();
        } catch (SendFailedException sfe){
            System.out.println(sfe);
        }
    }
 }

Hi, I am developing an Android application, and I would like to have the function of turn on mobile data automatically once the Wifi is detected not connected to the phone, because I would like to make sure the email can be sent out no matter the Wifi is connected or not... So once the wifi is detected not connected, the 3G data is turned on and the email is sent out and the data network turned off...

May I know how to perform the turn on 3G network and turn off 3G network??? The source on Internet is sparse and I hope anyone can help me to solve it... Thanks...

Answer

Naveen picture Naveen · Jul 2, 2013

Hope this code will help you ,In my case it worked.

ConnectivityManager dataManager;
dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(dataManager, true);        //True - to enable data connectivity .

in Manifest

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