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

标签 android audio notifications default

我的代码似乎有问题。 我已经创建了一个测试 Activity ,只是想看看有什么问题,但我还是做不到。

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);

}
}

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

我查看了手机的设置,一切正常,没有静音,默认声音已启用。

最佳答案

这...

notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.DEFAULT_VIBRATE;

应该是……

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;

关于android - 我的代码有什么问题 - 通知 - 没有声音没有振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616180/

相关文章:

ImageView 上的Android TextView

ios - 如何检测用户何时清除您应用的通知

android - 使用AudioTrack改善音符合成的性能

android - Phonegap notification.alert 仅适用于 Android

ios - 登机牌(pk.pass 文件)- 如何修复切换时区后 iPhone 提醒时间错误的问题

java - 如何运行所有测试用例,甚至之前的测试用例都是错误的

java - 即使应用程序未运行,如何每 10 分钟在后台获取位置信息?

java - Java (Android) 中文本的自动检测语言

xcode - 如何使用XCode播放.ogg文件

python - 在python中返回wavfile.read的类型