2

预习

我尝试使用以下代码设置声音,但它不起作用

Uri defaultRingtone = Uri.parse("android.resource://"+getPackageName()+"/raw/alarm");

NotificationChannel channel = new NotificationChannel(JayaGrocer.NOTIFICATION_CHANNEL, "Picker Channel 0", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Picker notification bla bla bla");
channel.enableLights(true);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
  .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
  .build();

channel.setSound(defaultRingtone, audioAttributes);

有人知道如何自动启用声音开关吗?

4

3 回答 3

0

实际上,没有办法以编程方式打开/关闭通知,我们可以使用以下代码执行此操作的唯一方法这将在 oreo 版本以下工作,因为 android 已限制以编程方式控制显示通知

public class CustomNotificationcontroller {

private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

public static void makeText(Context mContext, String message, int time) {
    if (isNotificationEnabled(mContext)) {
        Toast.makeText(mContext, message, time).show();
    } else {
        showCustomAlertDialog(mContext, message);
    }
}

private static boolean isNotificationEnabled(Context mContext) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        AppOpsManager mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = mContext.getApplicationInfo();
        String pkg = mContext.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass;
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod =
                    appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (int) opPostNotificationValue.get(Integer.class);
            return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid,
                    pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
                | InvocationTargetException | IllegalAccessException ex) {
        }
        return false;
    } else {
        return false;
    }
}
private static void showCustomAlertDialog(Context mContext, String message) {
    if (!(mContext instanceof Activity && ((Activity)mContext).isFinishing())) {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
        mBuilder.setMessage(message);
        mBuilder.setPositiveButton(mContext.getString(R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        mBuilder.setCancelable(true);
        AlertDialog alertDialog = mBuilder.create();
        alertDialog.show();
    }
}}

对于奥利奥,您可以直接 为此使用隐式意图

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
            startActivityForResult(intent, 5);
于 2019-09-17T07:39:50.530 回答
0

可以使用RingtoneManagerfrom获取默认铃声 uri package: android.media。但是对于通知,最好使用TYPE_NOTIFICATION

RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

于 2019-09-17T07:26:06.273 回答
0

这对我有用

private void myNotification(String message) {

        Intent intent = new Intent(this, NotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.correct_answer);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "MY_NO")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            if(soundUri != null){
                // Changing Default mode of notification
                notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                // Creating an Audio Attribute
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();

                // Creating Channel
                NotificationChannel notificationChannel = new NotificationChannel("MY_NO","sound_checker",NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(soundUri,audioAttributes);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
        }
        mNotificationManager.notify(0, notificationBuilder.build());
    }

您可能需要卸载应用程序才能更改声音设置,查看此处了解更多信息

于 2019-09-17T07:27:19.417 回答