我的代码有什么问题 - 通知 - 没有声音没有振动

Saa*_*iko 10 audio notifications android default

我的代码似乎有问题.我创建了一个测试活动,只是为了看错了,但我还是不能.

public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String extra = "test";

    NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, test.class);

    Notification notification = new Notification(R.drawable.icon, 
            extra, 
            System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent);
    notification.flags |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.DEFAULT_VIBRATE;
    notification.flags |= Notification.FLAG_INSISTENT;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    myNotificationManager.notify(33, notification);

}
}
Run Code Online (Sandbox Code Playgroud)

当通知弹出时,我没有声音和/或振动.

我查看了手机的设置,它们没问题,没有静音,默认声音启用.

Wil*_*ate 17

这个...

notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)

应该...

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)


小智 13

对于您可以使用的1行代码中的所有默认值(声音,振动和光):

notification.defaults = Notification.DEFAULT_ALL;
Run Code Online (Sandbox Code Playgroud)

这相当于

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)

确保在Manifest中为vibrate设置权限

<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)