简体   繁体   English

如何以编程方式禁用短信通知?

[英]How to disable SMS notification programmatically?

This not work: 这不起作用:

try {
    Context mmsContext = context.createPackageContext("com.android.mms", Context.CONTEXT_IGNORE_SECURITY);
    mmsContext.grantUriPermission(context.getPackageName(), 
    Uri.parse("file:///data/data/com.android.mms/shared_prefs/com.android.mms_preferences.xml"), 
    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mmsContext);
    Editor editor = settings.edit();
    editor.putBoolean("pref_key_enable_notifications", false);
    editor.commit();
} catch (NameNotFoundException e1) {
    e1.printStackTrace();
}

Error: "Attempt to read preferences file /data/data/com.android.mms/shared_prefs/com.android.mms_preferences.xml without permission" Help, please! 错误:“未经允许尝试读取首选项文件/data/data/com.android.mms/shared_prefs/com.android.mms_preferences.xml”,请帮助!

You cannot disable SMS notifications programmatically. 您不能以编程方式禁用SMS通知。 Please allow the user to decide whether or not SMS notifications should be enabled and allow them to control it through the appropriate settings screen. 请允许用户决定是否应启用SMS通知,并允许他们通过适当的设置屏幕进行控制。

By "you cannot", do you mean you should not , or there is no clean way of doing it/there's no API for that? 所谓“您不能”,是指您不应该这样做,还是没有干净的方法/没有API? Or do you really mean there is no way ? 还是您真的是说没有办法

In most cases, I fully agree with you that the user should be able to control it through the appropriate settings screen. 在大多数情况下,我完全同意您的看法,即用户应该能够通过适当的设置屏幕进行控制。 However, unfortunately, there are cases where there is no settings screen to toggle message notifications. 但是,不幸的是,在某些情况下,没有设置屏幕可以切换消息通知。 An example I know of is the Samsung Behold II (which replaces the default messaging app, and removes the option to set/unset message notifications). 我知道的一个例子是Samsung Behold II(它取代了默认的消息收发应用程序,并删除了设置/取消消息通知的选项)。 I'm trying to develop a way around this, as there have been several requests by Behold II owners who've installed alternative MMS/SMS apps, and do not want the double notification. 我正在尝试解决此问题的方法,因为已经安装了替代MMS / SMS应用程序的Behold II所有者提出了多个请求,并且不希望出现双重通知。 I've tried to access the preference activity directly using the following code, but it forced closed 我尝试使用以下代码直接访问首选项活动,但是它被强制关闭

    final Intent messagesettings = new Intent();
    messagesettings.setClassName("com.android.mms", "com.android.mms.ui.MessagingPreferenceActivity");
    ...
        public void onClick(View view){
            startActivity(messagesettings);
        }

Any ideas? 有任何想法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM