【问题标题】:What's wrong with my code - Notification - no sound no vibrate我的代码有什么问题 - 通知 - 没有声音没有振动
【发布时间】:2011-07-07 19:44:24
【问题描述】:

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

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

}
}

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

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

【问题讨论】:

    标签: android audio notifications default


    【解决方案1】:

    这...

    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;
    

    【讨论】:

      【解决方案2】:

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

      notification.defaults = Notification.DEFAULT_ALL;
      

      这相当于

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

      确保您在清单中设置振动权限

      <uses-permission android:name="android.permission.VIBRATE" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        相关资源
        最近更新 更多