无法为通知android设置铃声

Posted

技术标签:

【中文标题】无法为通知android设置铃声【英文标题】:Not able to set ringtone for notification android 【发布时间】:2014-03-13 13:40:25 【问题描述】:

我正在开发一个需要为通知设置铃声的应用程序,因为我正在使用铃声选择器,它在选择器上显示铃声列表,我可以选择铃声,但下次再次设置铃声时,铃声选择器不显示以前选择的铃声和通知都带有默认铃声。我陷入其中,无法找到任何解决方案。提前致谢。

这是我的代码

public class ActivitySettings extends Activity 
    Context mContext;
    // making TypeFace object global for setting custom font to various Views
    Typeface objTypeFace;
    // variables for toggle button values
    boolean notification;
    // declaring toggle button objects
    ToggleButton toggleNotification;
    // Key for Checking whteher notification is on or off
    public static final String PREFS_NOTIFICATION = "Notification";
    Uri uri1,ringtone;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_settings);
        mContext = this;
        // getting font object
        objTypeFace = Typeface.createFromAsset(getBaseContext().getAssets(),
                "fonts/HelveticaNeueLTStd-Md.ttf");
        // getting all textview objects from xml layout
        TextView txtHeading = (TextView) findViewById(R.id.Setting_txtHeading);
        txtHeading.setTypeface(objTypeFace);
        TextView txtNotification = (TextView) findViewById(R.id.Setting_txtnotification);
        txtNotification.setTypeface(objTypeFace);
        TextView txtTone = (TextView) findViewById(R.id.Setting_txtsettone);
        txtTone.setTypeface(objTypeFace);
        TextView txtSyncContact = (TextView) findViewById(R.id.Setting_txtSynContact);
        txtSyncContact.setTypeface(objTypeFace);
        // getting all buttons object from xml layout
        Button btnBack = (Button) findViewById(R.id.btnBack);
        toggleNotification = (ToggleButton) findViewById(R.id.Setting_togglenotification);

        // adding click events to various buttons
        btnBack.setOnClickListener(clickButtons);
        txtTone.setOnClickListener(clickButtons);
        toggleNotification.setOnClickListener(clickButtons);
        // getting values of toggle buttons from shared prefrence
        notification = CommonUtility.getNotificationFromPrefernce(mContext,
                PREFS_NOTIFICATION);
     // on create ends


@Override
    protected void onStart() 
        // TODO Auto-generated method stub
        super.onStart();
        // setting toggle buttons according to various values
        if (notification) 
            toggleNotification.setChecked(true);
         else 
            toggleNotification.setChecked(false);
        

     // method ends

    // Click Listener for various buttons
    public OnClickListener clickButtons = new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            switch (v.getId()) 
            case R.id.Setting_txtsettone:
                // code for setting notification tones


            Intent tmpIntent = new Intent(
                        RingtoneManager.ACTION_RINGTONE_PICKER);
                tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                        RingtoneManager.TYPE_NOTIFICATION);
                tmpIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                startActivityForResult(tmpIntent, 123);
                break;
            case R.id.btnBack:
                finish();
                break;
            case R.id.Setting_togglenotification:
                if (toggleNotification.isChecked()) 
                    notification = true;
                    CommonUtility.setNotificationPrefernce(mContext,
                            PREFS_NOTIFICATION, notification);
                 else 
                    notification = false;
                    CommonUtility.setNotificationPrefernce(mContext,
                            PREFS_NOTIFICATION, notification);
                    // Clear all notification
                    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    nMgr.cancelAll();
                
                break;

            
        
    ; // click events ends


    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        if (resultCode == RESULT_OK && rerequestCode==123) 
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);          
            if (uri != null) 
            String ringTonePath = uri.toString();
            RingtoneManager.setActualDefaultRingtoneUri(
                    mContext,
                    RingtoneManager.TYPE_RINGTONE,
                    uri);
            System.out.println("SElected tone uri is ///////"
                    + uri);
        
       

    

 // final class ends 

在 logcat 中我得到了这个错误,但在谷歌上我找不到任何解决方案

  03-13 19:33:55.189: E/DatabaseUtils(403): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
03-13 19:33:55.189: E/DatabaseUtils(403):   at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13090)
03-13 19:33:55.189: E/DatabaseUtils(403):   at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2038)
03-13 19:33:55.189: E/DatabaseUtils(403):   at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:607)
03-13 19:33:55.189: E/DatabaseUtils(403):   at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
03-13 19:33:55.189: E/DatabaseUtils(403):   at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
03-13 19:33:55.189: E/DatabaseUtils(403):   at android.os.Binder.execTransact(Binder.java:388)
03-13 19:33:55.189: E/DatabaseUtils(403):   at dalvik.system.NativeStart.run(Native Method)
03-13 19:33:55.319: D/AbsListView(19933): unregisterIRListener() is called 

【问题讨论】:

【参考方案1】:

好吧,我没有查看您的代码,但我的第一个猜测是 logcat 输出,它几乎可以准确地告诉您出了什么问题。您缺少所需的权限:

...this requires android.permission.INTERACT_ACROSS_USERS_FULL

编辑:好的,我检查了权限,这很糟糕。您将无法使用此权限,因为它是签名级别的权限。您在代码中使用了对普通开发人员有限制的东西。

【讨论】:

我已将此权限添加到我的应用程序 manifest.xml 但它不起作用,伙计 是的,我已经注意到并编辑了我的答案,请发布更多您的 logcat 输出。 你还能发更多吗?我需要看看你到底在哪里遇到了权限问题,为此我需要在 SecurityException 之前和之后看到很多 logcat 输出。【参考方案2】:

您可能已经解决了这个问题,但对于任何感兴趣的人 - 我也遇到过同样的问题(这在三星设备上似乎更常见),我发现的解决方案是授予 READ_EXTERNAL_STORAGE 权限。

附带说明:INTERACT_ACROSS_USERS_FULL 似乎只有在您的应用程序是本机的情况下才能工作,否则我

【讨论】:

以上是关于无法为通知android设置铃声的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android中设置铃声+震动

Android为用户自定义号码设置自定义铃声

Android之来电铃声设置

如何通过我的活动在 Android 中设置铃声?

如何在我的活动中设置Android铃声?

在 Android 7(API 级别 24)中,我的应用不允许将手机静音(将铃声模式设置为静音)