I want to disable data connection and I am using this code.
ConnectivityManager dataManager;
dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
try {
dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataMtd.setAccessible(true);
try {
dataMtd.invoke(dataManager, false);
} catch (IllegalAccessException 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();
}
this will not work in dual sim. so can someone help me.
how can I disable data connection in dual sim android phone?
and how can i check if phone is dual sim or not
Some things to mention here:
before Android 5.1 there was no official API for dual sim functionality. Thus there seems to be no universal solution for older devices. Since 5.1 an API is available.
your code will no longer work on Android 5+, as pointed out in this question
however, this answer provides a solution to both bullets above: the method setMobileNetworkfromLollipop checks whether target is 5 or 5.1+, and in case of 5.1+ it loops through all subscription id's (=sim cards) to switch data services. You could combine it with your code to target previous versions as well. The downside: it requires root access, and dual-sim functionality is limited to 5.1+.
Related
i am using openfire xmpp server and asmack library to implement pubub node , the biggest issue that i face is the reconnection problem to the xmpp server.
whenever a connection is made to the xmpp server there are lots of stanza is being exchanged possibly around 5 to 7 stanza in a desktop applicaiton or web application it seems ok , but for mobile application where 3G link is pretty weak , in that case it too good to exchange so much of stanza.
i have tested that when the wifi has a weak signal or 3g link is down , i am making a reconnecting to the server (in background process) but i mostly get a response time out error and taking too much time if the connection gets successful.
i have seen messenger like whatsapp in which they are making a reconnection pretty fast. i want to do something like that only.
i have read about the pre-http binding but its exist in ejabber but didnt find anything in openfire moreover htt-prebinding is for anonymous users and i am using registered users only.
so can anyone tell me how can i reconnect fast to the xmpp server.
code snippet
Boolean onlyConnect = arg[0];
try {
if(xmpp!= null && !xmpp.isConnected())
xmpp.connect();
} catch(BOSHTimeoutException e){
Log.e("XConnection " , "Timeout occures");
// make a reconnection here
return false;
}
catch(ConnectionException e){
CatchErrors("ConnectionException" , "xmpp.connect()");
e.printStackTrace();
}catch(NoResponseException e){
CatchErrors("NoResponseException" , "xmpp.connect()");
// make a reconnection here
return false;
}catch (XMPPException e1) {
// TODO Auto-generated catch block
CatchErrors("XMPPException" , "xmpp.connect()");
e1.printStackTrace();
return false;
} catch (SmackException e) {
CatchErrors("SmackException" , "xmpp.connect()");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
CatchErrors("IOException" , "xmpp.connect()");
e.printStackTrace();
}
try {
Log.e("LOGIN REQUIRED " , "LOGIN IN");
xmpp.login(XMPP_USER_NAME, XMPP_PASSWORD, "sh");
} catch (XMPPException e) {
// TODO Auto-generated catch block
CatchErrors("XMPPException" , "xmpp.login()");
e.printStackTrace();
return false;
} catch(NoResponseException e){
Log.e("LOGIN REQUIRED " , "NoResponseException");
CatchErrors("NoResponseException" , "xmpp.login()");
//make a reconnection here
return false;
}catch (SaslException e) {
// TODO Auto-generated catch block
CatchErrors("SaslException" , "xmpp.login()");
e.printStackTrace();
} catch (SmackException e) {
// TODO Auto-generated catch block
CatchErrors("SmackException" , "xmpp.login()");
e.printStackTrace();
} catch (IOException e) {
CatchErrors("IOException" , "xmpp.login()");
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
How can I make my Android app compatible with previous Android versions if I use API from the view class like setLayerType that are not on the Android 2.3.3 API? What can I substitute this method with?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
// only for gingerbread and newer versions
// setLayerType
}
or through reflection
try {
Method setLayerTypeMethod = mWebView.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
if (setLayerTypeMethod != null)
setLayerTypeMethod.invoke(yourView, new Object[] {LAYER_TYPE_SOFTWARE, null});
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
to disable hardware acceleraton you can use android:hardwareAccelerated="false"
more detail here: http://developer.android.com/guide/topics/graphics/hardware-accel.html#controlling
but pay attention with android:hardwareAccelerated="false" because you will pay the cost with app performance
i think the best deal is to use reflection as described by blackbelt
I don't find many BluetoothDevice methodes such as , setPasskey(), setPin(), setPairingConfirmation(), setRemoteOutOfBandData().
I searched on Android site as well but I don't find it. When I use these methods in my program in eclipse it shows me an error: its undefined for the type BluetoothDevice.
Are these obsolete now? If yes then what are the new methods of same type.
It is assumed that paring process is performed only by applications delivered with a platform!
This means that this application have access to hidden API. For example you can find hidden API for Bluetooth here.
It is strongly recommended to not use hidden API since it can change without warning in next Android release.
If you are still planning to use this API safest way is to use reflection:
try {
Class<? extends BluetoothDevice> c = device.getClass(); // BluetoothDevice.class
Method createBond = c.getMethod("createBond");
Object result = createBond.invoke(device);
Boolean castedResult = (Boolean)result;
Log.d(TAG, "Result: " + castedResult.toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
There is also alternative way to easy use hidden API, but I didn't try it.
I want to check if the method Camera.Parameters.getHorizontalViewAngle() exists on the device (it's only available from API 8 and my min SDK API is 7). I tried to use "reflection", as explained here, but it catches an error saying the number of arguments is wrong:
java.lang.IllegalArgumentException: wrong number of arguments
Anybody could help?
Camera camera;
camera = Camera.open();
Parameters params = camera.getParameters();
Method m = Camera.Parameters.class.getMethod("getHorizontalViewAngle", new Class[] {} );
float hVA = 0;
try {
m.invoke(params, hVA);
} 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();
}
m.invoke(params, hVA);
should be
m.invoke(params, null);
Camera.Parameters.getHorizontalViewAngle() doesn't take any arguments and the above line has the argument hVA. If you're looking for the return variable do hVA = m.invoke(params, null);
Personally, I recommend conditional class loading, where you isolate the new-API code in a class that you only touch on a compatible device. I only use reflection for really lightweight stuff (e.g., finding the right CONTENT_URI value to use for Contacts or ContactsContract).
For example, this sample project uses two implementations of an abstract class to handle finding a Camera object -- on a Gingerbread device, it tries to use a front-facing camera.
Or, this sample project shows using the action bar on Honeycomb, including putting a custom View in it, while still maintaining backwards compatibility to older versions of Android.
I know this is a hack, but why can't you put the first call to the method in a try/catch of it's own, and nest the rest of your try/catch code in there. If the outer catch executes, the method doesn't exist.
I'm new to programming Android devices. I'm making an school project which involves an Android (Mini-Xperia pro with Android 2.1) and bluetooth communication with a device.
I'm trying to go step by step to undesrstand all of the programming stuff and to learn all I can.
I've got an Bluetooth adapter for the PC, I'm working with Windows XP so I only connect it and it's already installed.
Well, I'm working over the Bluetooth Chat sample that comes with the SDK and I've already changed the UUID to:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
With my mobile I entered to settings and made my device paired to my Pc (it says paired but with out conection).
But I don't know what I'm doing wrong cause I open the Bluetooth chat application on my mobile, try to connect to my pc and it says "unable to connect device".
After a lot of tries, it connects to transmit from the pc to the phone:
A
AT
And the connection is lost (this takes less than 2 seconds!!)
Can anyone help me please tell me what am I doing wrong or what's the problem??
Thanks.
You need to change the ConnectThread code to the following: Note the change code which creates the socket.
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
Method m = null;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] {int.class});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = (BluetoothSocket) m.invoke(device, 1);
} 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();
}
mmSocket = tmp;
}
you need to run some application like a hyper-terminal on the PC side over the Bluetooth Serial COM port to which the android application is connecting.