6. Cancel Missed Calls Notification

In the android source, we can found that a project "Contacts" that has an activity "RecentCallsListActivity". When that activity got focus, it will cancel the missed calls notification by below code:

cancelMissedCallsNotification

770             try {
771                 ITelephony iTelephony =
772                         ITelephony.Stub.asInterface(ServiceManager.getService("phone"));
773                 if (iTelephony != null) {
774                     iTelephony.cancelMissedCallsNotification();
775                 } else {
776                     Log.w(TAG, "Telephony service is null, can't call " +
777                             "cancelMissedCallsNotification");
778                 }
779             } catch (RemoteException e) {
780                 Log.e(TAG, "Failed to clear missed calls notification due to remote exception");
781             }
If we just paste above code to our android program, we will never be able to compile the code. It is because it used some android internal classes "android.os.IBinder", "android.os.ServiceManager" and "com.android.internal.telephony.ITelephony". Those classes is not available in the android SDK. So, I use java reflection to access such classes. Below are the rewrote code:

cancelMissedCallsNotification

    private void cancelMissedCallsNotification() {
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("TTT", "Telephony service is null, can't call "
+ "cancelMissedCallsNotification");
}
} else {
Log.d("TTT", "Unable to locate ITelephony.Stub class!");
}
} catch (ClassNotFoundException ex) {
Log.e("TTT", "Failed to clear missed calls notification due to ClassNotFoundException!", ex);
} catch (InvocationTargetException ex) {
Log.e("TTT", "Failed to clear missed calls notification due to InvocationTargetException!", ex);
} catch (NoSuchMethodException ex) {
Log.e("TTT", "Failed to clear missed calls notification due to NoSuchMethodException!", ex);
} catch (Throwable ex) {
Log.e("TTT", "Failed to clear missed calls notification due to Throwable!", ex);
}
    }
To get the method work correctly, you must gain MODIFY_PHONE_STATE permission in AndroidManifest.xml
Comments