I am working on application which will use the Telephony API.
I am using:
Context context = getBaseContext();
TelephonyManager tMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
try {
Class<?> c = Class.forName(tMgr.getClass().getName());
Method method = c.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(tMgr);
telephonyService.silenceRinger();
telephonyService.answerRingingCall();
} catch (Exception e) {
// exception handling
}
This code is used to auto answer call, and works fine in Android 2.2, but doesn't work in Android 2.3.
Is there any replacement of Telephony API in Android 2.3?
in your case its only the permission that changed from MODIFY_PHONE_STATE to READ_PHONE_STATE and if you want to accerss if via broadcastreceiver from outside your app, you'll need permission GET_TASK too ....
Related
I need to Turn ON/OFF Mobile data programmatically. Below code is not working for 5.x. Can you please help me. Thanks in advance.
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled); }
03-30 12:42:29.466: W/System.err(5966):
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] 03-30
12:42:29.466: W/System.err(5966): at
java.lang.Class.getMethod(Class.java:664) 03-30 12:42:29.466:
W/System.err(5966): at
java.lang.Class.getDeclaredMethod(Class.java:626)
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] # below line.
final Method setMobileDataEnabledMethod =
connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",
Boolean.TYPE);
It seems like the setMobileDataEnabled method no longer exists in
ConnectivityManager and this functionality was moved to
TelephonyManager with two methods getDataEnabled and setDataEnabled.
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
When executing the code you get a SecurityException stating that Neither user 10089 nor current process has android.permission.MODIFY_PHONE_STATE.
A permission MODIFY_PHONE_STATE should be added
I got this from Answer
Thank you Muzikant
In Android L 5.xx the hidden API setMobileDataEnabled method is removed and it can no longer be used. You can verify this in android lolipop source code under /frameworks/base/core/java/android/net/ConnectivityManager.java.
If you still insist to perform it, you can use code snippet answered by Kushal but getDataEnabled is a system api, which normal user applications cant access. There is also one more system api available setDataEnabled under TelephonyManager. (/frameworks/base/telephony/java/android/telephony/TelephonyManager.java)
/** #hide */
#SystemApi
public void setDataEnabled(boolean enable) {
setDataEnabled(SubscriptionManager.getDefaultDataSubId(), enable);
}
It also needs the permission "android.permission.MODIFY_PHONE_STATE" which will work only on rooted devices.
Quick response, it is not possible to enable/disable mobile data by programming as it is possible with bluetooth.
Starting from version 23 of android, data protection begins to be highly valued and also the protection that an application activates or disables something on the phone, for this reason the permissions are implemented, taking this into account
Starting from this, google proposes that you encourage the user to do this action, giving him context and why he needs to have it activated.
Redirect the user to activate the data
Intent intent = new Intent(Settings.ACTION_DATA_USAGE_SETTINGS);
startActivity(intent);
Redirect the user to activate gps
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Request enable Bluetooth
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
I making a phone call cancel app. Basically it cancel a upcoming call if your phone is in back position in table or ground (Accelerometer Data). I make a Broadcast Receiver and entry it on the manifest also gave action this android.intent.action.PHONE_STATE
public class PhoneCallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent si=new Intent(context, MyService.class);
context.startService(si);
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
//telephonyService.silenceRinger();
telephonyService.endCall();
Log.e("in try catch", "yes");
Log.e("in try catch", "call cancel");
shrededit.putInt("newcallingstate", 0);
shrededit.commit();
context.stopService(si);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("pr", "out side true block");
}
}
My code is running very well if i only use this code without the accelerometer service class but when i use accelerometer class and make a intent before this below code. My app not canceling the call or not giving any type or error. I think it but not completely sure it is context problem.
So please help me.
No,its not context problem all this code is perfectly fine you does just need a condition or use sharedpreferences for condition.
first make a sharedpreferences object in your service class or that which you show in intent. like this
SharedPreferences sharedpref=this.getSharedPreferences("your_file_name",Context.MODE_PRIVATE);
SharedPreferences.Editor sharededit=sharedpref.edit();
sharededit.putBoolean("String_name", true);
sharededit.commit();
Here boolean is not important you can choose and other data type it depends you.
Then in you Broadcast receiver class you have to again use these SharedPreference and SharedPreference.Editor function and you get the previous boolean value by this method
boolean value=sharededit.getBoolean("callingStop", true);
Than make a condition like this
if(value==true){TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
//telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
} }
This is all now your code is run without any error.
I'm having problem in phone state listener. I want to call one activity from onCallStateChanged event. When i get an incoming call i want to call one activity and process the transaction of the current phone no(which i currently get by incoming call).
But i couldn't go to ShowPhoneStateDialogActivity activity using following code.Please correct me what is my mistake. Thanks in advance.
My code is,
case TelephonyManager.CALL_STATE_RINGING:
Log.d("PHONE:", "RINGING");
Log.w("Call STATE:", "RINGING");
if (!sess.getCallActive()) {
sess.setCallActive(true);
sess.setActiveMobileNo(incomingCallNumber);
this.endActivecall();
Intent intent = new Intent(context,ShowPhoneStateDialogActivity.class).setAction("incomingNumber");
intent.putExtra("Phoneno", incomingCallNumber);
Log.i("CURRENT ACTIVITY",this.getClass().getSimpleName());
Log.i("CURRENT CONTEXT","Context:"+context);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intent);
break;
} else {
this.endActivecall();
}
break;
And endActivecall function is,
public void endActivecall() {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
// telephonyService.silenceRinger();
telephonyService.endCall();
Log.i("CALL STATE ACTION:", "Call end");
Log.i("ACTIVE_MOBILENO:", sess.getActiveMobileNo());
} catch (Exception e) {
e.printStackTrace();
}
Instead of the code context.startService(intent), try calling context.startActivity(intent) instead.
I use this code to block a incomming Calls in android Application..
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.silenceRinger();
telephonyService.endCall();
This Code is not work Properly.. At least one time ringging a phone and after cut calls..
Please.. Some one Give me better solution..
Thanks to u. Problem is that at least one time ringging device and after cut calls so not cut calls but missed calls..
i have used the following code:
Problem: It is working fine and blocking incoming call but phone ring for fraction of a second before disconnect the call. can there be a solution which i am looking for .
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
incomingNumber1 = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager telephony = (TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}}
I have not found the answer to the problem yet. I want solution which work perfectly on Android 2.3. Incoming call is to be blocked fully there must not any ring.