i'm trying to use the method setMobileDataEnabled of the ConnectivityManager class, using SDK 2.2.
According to http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source this method is declared public but with #hide is not available in the SDK and in Eclipse.
In order to bypass the hiding I wrote the following function to toggle the mobile data connection on/off.
public void setMobileData(boolean toBeEnabled){
Object myObj= getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) myObj;
Class c = null;
try {
c = Class.forName(cm.getClass().getName());
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getMobileDataEnabled");
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (NoSuchMethodException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Object mobileDataEnabled=null;
if (m!=null){
m.setAccessible(true);
Type res_of_m= m.getGenericReturnType();
Type[] pars_of_m= m.getGenericParameterTypes();
try {
mobileDataEnabled = (m.invoke(cm));
if (mobileDataEnabled!=null)
if (mobileDataEnabled.equals(!toBeEnabled)){
Method m2 = null;
try {
int index=0;
boolean method_found=false;
Method[] available_methods= c.getDeclaredMethods();
for (Method method : available_methods) {
// following line doesn't work
// method.getName()=="setMobileDataEnabled"
if (method.getName().contains("setMobileDataEnabled"))
{
method_found=true;
}
if (method_found==false)
index++;
}
// following line doesn't work
//m2 = c.getDeclaredMethod("setMobileDataEnabled");
m2 = (c.getDeclaredMethods())[index];
if (m2!=null){
m2.setAccessible(true);
m2.invoke(cm,toBeEnabled);
}
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InvocationTargetException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To make it working I also added the android.permission.WRITE_SECURE_SETTINGS" in the manifest and installed in /system/app according to Android: Add app to firmware, use WRITE_SECURE_SETTINGS.
Does anyone know a better way?
try {
final ConnectivityManager cm = (ConnectivityManager) this.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class connectivityManager = Class.forName(cm.getClass()
.getName());
final Method[] methods = connectivityManager.getDeclaredMethods();
for (final Method method : methods) {
if (MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
.equals(method.getName())) {
method.invoke(cm, true);
}
}
} catch (final ClassNotFoundException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalArgumentException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalAccessException e) {
Log.e("Class", e.getMessage());
} catch (final InvocationTargetException e) {
Log.e("Class", e.getMessage());
}
Related
hello i am creating three different type of modules for mobile where the user can choose any of them by clicking a button.Now each module has its set of option to accept or reject calls.When for a particular module user selects reject then i am storing a Boolean value in database.
I want that whenever an user has selected call reject and checking out of that module to select another module then that call reject functionality should automatically stop.
Now,how to automatically unregister the broadcast receiver that rejects call.
CallBlocker = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//Java Reflections
Class c = null;
try {
c = Class.forName(telephonyManager.getClass().getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getITelephony");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.setAccessible(true);
try {
telephonyService = (ITelephony) m.invoke(telephonyManager);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
}//onReceive()
PhoneStateListener callBlockListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
if (autoreject==1) {
try {
telephonyService.endCall();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
unregisterReceiver(CallBlocker);
}
}
}
};
};/
I think this could help you out:-
How to reject/close specific incoming number
How to reject a call programatically android
https://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/#comments
I need to programmatically end an Incoming Call when a button from my App is being clicked.
If I use TelephonyService, it is working fine below 4.0 versions. But it is not working in 4.2.2 version.
TelephonyManager telephony = (TelephonyManager) srvs
.getSystemService(Context.TELEPHONY_SERVICE);
Class c;
final com.android.internal.telephony.ITelephony telephonyService;
try {
c = Class.forName("android.telephony.TelephonyManager");//telephony.getClass().getName());
Log.i("TelephonyClass Name", telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
TimerTask task = new TimerTask() {
#Override
public void run() {
try {
if (telephonyService.isIdle()
|| telephonyService.isOffhook()
|| telephonyService.isRinging())
telephonyService.endCall();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
long delay = Integer.parseInt(cutofftime) * 1000;
new Timer().schedule(task, delay);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'm trying to save a custom object when I click a menu item. The problem is its crashing. Before we start, when I try to read that data in another activity, that also crashes. Here is my code where I'm trying to save the data:
Here's a pastebin link.
my write function in the menu
myInfo.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Intent ourIntent = new Intent(Results.this, listTimes.class);
ourIntent.putExtra("Meeting", meetingObj);
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(businesses.get(positionChecked));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//startActivity(ourIntent);
return true;
}
// My read function
private void getData()
{
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
business = (Business) ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I want to enable my 3g DataConnection through code, without any user interaction. Everything must be done in background itself.
I tried all the codes I found in Google, but nothing works in my Android Tablet 4.0.4.
I inserted the 3g sim-card, restarted the device. But the code will not automatically invoke the DataConnection in Tablet.My Tablet internal memory name will be "SDCard2", for this reason is there any problem with that code.
Please give me Appropriate solution for this.
I used below code :
public static void EnableInternet(Context mycontext)
{
try {
Log.i("Reached Enable", "I am here");
Process proc;
try {
proc = Runtime.getRuntime().exec( "su" );
try {
proc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setMobileDataEnabled(mycontext,true);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void turnData(boolean ON)
{
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method iMthd = null;
try {
iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (Exception e) {
}
iMthd.setAccessible(false);
if(ON)
{
try {
iMthd.invoke(iMgr, true);
Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
// dataButton.setChecked(false);
Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
// dataButton.setChecked(false);
Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();
}
}
else
{
try {
iMthd.invoke(iMgr, true);
Toast.makeText(getApplicationContext(), "Data connection Disabled", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// dataButton.setChecked(true);
Toast.makeText(getApplicationContext(), "Error Disabling Data connection", Toast.LENGTH_SHORT).show();
}
}}
boolean switchState(boolean enable)
{
boolean bRes = false;
// Data Connection mode (only if correctly initialized)
if (m_telManager != null)
{
try
{
// Will be used to invoke hidden methods with reflection
Class cTelMan = null;
Method getITelephony = null;
Object oTelephony = null;
Class cTelephony = null;
Method action = null;
// Get the current object implementing ITelephony interface
cTelMan = m_telManager.getClass();
getITelephony = cTelMan.getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
oTelephony = getITelephony.invoke(m_telManager);
// Call the enableDataConnectivity/disableDataConnectivity method
// of Telephony object
cTelephony = oTelephony.getClass();
if (enable)
{
action = cTelephony.getMethod("enableDataConnectivity");
}
else
{
action = cTelephony.getMethod("disableDataConnectivity");
}
action.setAccessible(true);
bRes = (Boolean)action.invoke(oTelephony);
}
catch (Exception e)
{
bRes = false;
}
}
return bRes;
}
try {
ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(mgr, true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I used below permissions also i Manifest file :
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
You need to have the instance of IConnectivityManager class from the connectivityManager object and then use the setMobileDataEnabled() method on the iConnectivityManager instance.
Try the code below (tested on Galaxy Y (2.3) and Nexus 4 (4.2)) :
public void cellularState(boolean shouldEnable) {
ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class<?> conmanClass = Class.forName(conman.getClass().getName());
Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
Object iConnectivityManager = iConnectivityManagerField.get(conman);
Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, shouldEnable);
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In manifest declare:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
i would like to get data from my host server by using a data connection in mobile.
when my app is open it should connect to host server by data connection automatically.
when in case of connection lost ... my service should automatically connect to the host sever by enabling the data connection from mobile not using with Bluetooth or WiFi.
please help me from this situation
in my code i am using to check whether data connection is
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//some task
return true;
}
else{
Toast.makeText(this, "No internet connection", Toast.LENGTH_SHORT).show();
}
return false;
Use this class as it is and call it methods when ever you want to Enalbe /Disable DataConnection
package com.AZone.eabc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import android.net.ConnectivityManager;
import android.util.Log;
import android.content.Context;
public class InternetControl {
public static void EnableInternet(Context mycontext)
{
try {
Log.i("Reached Enable", "I am here");
setMobileDataEnabled(mycontext,true);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void DisableInternet(Context mycontext)
{
try {
Log.i("Reached Disable", "I am here");
setMobileDataEnabled(mycontext,false);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void setMobileDataEnabled(Context context , boolean enabled) throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
}
Call methods of this class from AnyWhere like this
InternetControl.EnableInternet(getBaseContext());
ADD following permissions in You AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
Looks like it doesn't work on some phones witn Android 4.1.X.
I've been using this approach for a long time but it crashes on Samsung 5282 Android 4.1.2:
"NoSuchFieldException: mService"
Looks like Samsung removed mService field by itself because it still can be found in the GrepCode:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/net/ConnectivityManager.java/
Use this method to check,Is your device connected to Internet or not.
public boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
Put this inside the click event:
boolean Connection;
Connection = checkInternetConnection();
if(Connection==false){
//No internet connection
}
else{
//Here do what ever you do next
}