android - 更新 Android 上的未接来电通知

标签 android

我需要取消某个号码的未接来电通知。我在 com.android.phone 上看到了 NotificationMgr 类,但我无法通过反射调用它。还有其他办法吗?

最佳答案

下面的代码将取消未接来电通知。

要使该方法正常工作,您必须在 AndroidManifest.xml 中获得 MODIFY_PHONE_STATE 权限,例如

    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>

在你的 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);
    }

原文链接为 http://sites.google.com/site/t2k269group/development-diary/reset-missed-calls-notification

如果有人知道如何使用反射访问com.android.phone中的类,请告诉我。

关于android - 更新 Android 上的未接来电通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2720967/

相关文章:

android - 从Firestore中删除文档后,如何实时更新Recyclerview?

android - 如何将Paho-MQTT添加到android studio

android - 错误 : 'to_string' is not a member of 'std'

android - 为 Bintray 上传禁用 javadoc 检查

android - 子类房间实体

Android - 如果在 Android 5.1.1 上通过 gradle 构建,安装调试版本 apk 失败(但在 Android 6.0.1 上没问题)

android - 处理网络断开的最佳方式

android - 从 fbsdk React Native 获取数据

android - 产品 flavor 【从eclipse迁移到android studio】

android - NFC 智能海报的 Intent 过滤器