I need to cancel the missed calls notification for a certain number. I've seen the NotificationMgr class on com.android.phone but i'm unable to call it trough reflection. Is there any other way?
The code below will cancel the missed call notification.
To get the method work correctly, you must gain MODIFY_PHONE_STATE permission in AndroidManifest.xml like
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
in your AndroidManifest.xml
String Log_Tag = "log";
try
{
Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getServiceMethod = serviceManagerClass.getMethod("getService", String.class);
Object phoneService = getServiceMethod.invoke(null, "phone");
Class ITelephonyClass = Class.forName("com.android.internal.telephony.ITelephony");
Class ITelephonyStubClass = null;
for(Class clazz : ITelephonyClass.getDeclaredClasses())
{
if (clazz.getSimpleName().equals("Stub"))
{
ITelephonyStubClass = clazz;
break;
}
}
if (ITelephonyStubClass != null)
{
Class IBinderClass = Class.forName("android.os.IBinder");
Method asInterfaceMethod = ITelephonyStubClass.getDeclaredMethod("asInterface",
IBinderClass);
Object iTelephony = asInterfaceMethod.invoke(null, phoneService);
if (iTelephony != null)
{
Method cancelMissedCallsNotificationMethod = iTelephony.getClass().getMethod(
"cancelMissedCallsNotification");
cancelMissedCallsNotificationMethod.invoke(iTelephony);
}
else
{
Log.w(LOG_TAG, "Telephony service is null, can't call "
+ "cancelMissedCallsNotification");
}
}
else
{
Log.d(LOG_TAG, "Unable to locate ITelephony.Stub class!");
}
} catch (ClassNotFoundException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to ClassNotFoundException!", ex);
} catch (InvocationTargetException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to InvocationTargetException!",
ex);
} catch (NoSuchMethodException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to NoSuchMethodException!", ex);
} catch (Throwable ex)
{
Log.e(LOG_TAG, "Failed to clear missed calls notification due to Throwable!", ex);
}
The original link is
http://sites.google.com/site/t2k269group/development-diary/reset-missed-calls-notification
If some know how to use the reflection to access class in com.android.phone, please tell me.
You cannot affect anyone other application's Notifications, let alone one for missed calls.
Related
Recently I modified my Android code for Android-12.
I was not having android-12 related bluetooth connect permissions for my API. So I handled as below :
public List<BluetoothDevice> getHfpDevices() {
List<BluetoothDevice> hfpConnectedDevs = new ArrayList<>();
try {
if (mHeadsetService != null) {
hfpConnectedDevs = mHeadsetService.getConnectedDevices();
}
} catch (SecurityException ex) {
Log.e(TAG, "Security Exception in Android-12", ex);
}
return hfpConnectedDevs;
}
Does this catch the securityException thrown when this API is accessed ? or do I need to handle the Exception itself ?
catch (Exception ex) {
PS : I don't want to add the requestPermission flow as of now. I just need to solve the exception raised when this API is called
I can't find in the doc how to retrieve if device is configured as a portable hotspot.
I've read How to detect if a network is (configured as) a mobile hotspot on Android? but I don't know if the feature has been implemented?
You can use below code to check if Tethering is enabled or not on your device:
private static Method isWifiApEnabledMethod;
public static boolean isWifiApEnabled(WifiManager wifiManager) {
if (isWifiApEnabledMethod == null) {
try {
isWifiApEnabledMethod = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
isWifiApEnabledMethod.setAccessible(true); //in the case of visibility change in future APIs
} catch (NoSuchMethodException e) {
Log.w(TAG, "Can't get method by reflection", e);
}
}
if (isWifiApEnabledMethod != null) {
try {
return (Boolean) isWifiApEnabledMethod.invoke(wifiManager);
} catch (IllegalAccessException e) {
Log.e(TAG, "Can't invoke method by reflection", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "Can't invoke method by reflection", e);
}
}
return false;
}
Don't forget to add below permission in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
For the SO link mentioned above in question:
This feature is not available i.e. has not been implemented.
For more information check this link where Google officially marked the status as Status: Won't Fix (Obsolete)
Hope this will help you. Thanks!
I'm try send DTMF codes in icoming CALL. For this i'n try use Java reflection:
public void initialize(){
ClassLoader classLoader = Dtmf.class.getClassLoader();
final Class<?> classCallManager = classLoader.loadClass("com.android.internal.telephony.CallManager");
Method methodGetInstance = classCallManager.getDeclaredMethod("getInstance");
objectCallManager = methodGetInstance.invoke(null);
methodGetState = classCallManager.getDeclaredMethod(SEND_DTMF, char.class);
}
public boolean sendDtmf(char ch) {
boolean result = false;
if ( methodGetState != null) {
try {
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
if (res instanceof Boolean) {
result = ((Boolean) res).booleanValue();
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
Link for source code of class CallManager : Call Manager source code
But i'm always get "false" in method sendDtmf(). In debug, code is go into next:
Object res = methodGetState.invoke(objectCallManager,
new Object[]{Character.valueOf(ch)});
What wrong?
The method is likely throwing an InvocationTargetException if your application isn't signed with the platform certificate as conventional apps cannot execute these methods (and will not be granted the required platform permissions to do so).
In short: the method is returning false because you're catching (and ignoring) the exception.
There's an open issue (#1428) on the Android issue tracker for sending DTMF tones as it presently isn't possible.
Case 1: Look at the code below. I am able to get Class and Method objects and it works well. Method I am trying to access is android.view.View::dispatchPointerEvent.
Case 2: When I replace class/Method with com.android.server.pm.PackageManagerService::grantPermissionsLPw, I get NoMethodFoundException. Class was accessible though.
Case 3: When I replace class/Method with android.hardware.input.InputManager::injectInputEvent, I get NoMethodFoundException. Class was accessible though.
Question is: Why some of the android class/methods are accessible via reflection and some other not?
Class _class = null;
try {
_class = Class.forName("android.view.View");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
method = _class.getDeclaredMethod("dispatchPointerEvent",
MotionEvent.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
//e.printStackTrace();
}
Try this
Class _class = null;
try {
_class = Class.forName("com.android.server.pm.PackageManagerService");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
Class _class2 = Class.forName("android.content.pm.PackageParser$Package");
method = _class.getDeclaredMethod("grantPermissionsLPw",
_class2, boolean.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
e.printStackTrace();
}
(Sorry but I cannot post comments, so have to post response)
Did you put the right method parameters? Where you have MotionEvent.class.
android.hardware.input.InputManager::injectInputEvent requires the android.permission.INJECT_EVENTS permission which is a system permission not available to apps. If you need to do this you'll have to root the device and sign your app as a system app.
I'm using aidl to answer call automagically, code as following:
ITelephony.Stub.asInterface(ServiceManager.getService("phone"))
.answerRingingCall();
I import ServiceManager.class
import android.os.ServiceManager;
but there's a problem:The import android.os.ServiceManager cannot be resolved
How can I make it work? Thanks
android.os.ServiceManager is a hidden class (i.e., #hide) and hidden classes (even if they are public in the Java sense) are removed from android.jar, hence you get the error when you try to import ServiceManager. Hidden classes are those that Google does not want to be part of the documented public API.
Applications using non-public API cannot be compiled easily, there will be different platform versions of this class.
Though it is old one, but no one has answered it yet. Any hidden classes can be used using reflection APIs. Here is an example to acquire a service using Service Manager via reflection APIs:
if(mService == null) {
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "My_SERVICE_NAME");
if(binder != null) {
mService = IMyService.Stub.asInterface(binder);
}
if(mService != null)
mIsAcquired = true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Service is already acquired");
}
As said above these methods work only on System apps or framework apps from Android N on words.
Still we can code for System app for ServiceManager usage as below using reflection of Android Code
#SuppressLint("PrivateApi")
public IMyAudioService getService(Context mContext) {
IMyAudioService mService = null;
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "YOUR_METHOD_NAME");
if (binder != null) {
mService = IMyAudioService .Stub.asInterface(binder);
}
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return mService;
}