代码之家  ›  专栏  ›  技术社区  ›  SirDarius

如何使用Android通知api启用振动和灯光?

  •  17
  • SirDarius  · 技术社区  · 14 年前

    // notification
    Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    // parameters
    String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
    if (ringtone.length() > 0) {
        notification.sound = Uri.parse(ringtone);
        notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
    }
    
    boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
    if (useVibrator) {
        notification.defaults |= Notification.DEFAULT_VIBRATE;
    }
    
    boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
    if (useLed) {
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    }
    
    // alert
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
    contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
    contentView.setTextViewText(R.id.notification_title, title);
    contentView.setTextViewText(R.id.notification_text, text);
    notification.contentView = contentView;
    
    Intent notificationIntent = new Intent(context, MyActivity.class);
    
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    
    notificationManager.notify(1, notification);
    

    通知有效,并使用正确的铃声。

    但是,即使正确地激活了首选项并且正确地设置了通知标志(我通过调试进行了检查),通知也不会振动,也不会导致灯被激活。

    我可能会责怪我的手机设置,但其他所有使用通知的应用程序,如短信、gmail和其他应用程序都正确地使用了所有这些功能。

    有人知道我做错了什么吗(我的手机是安卓2.1的HTC英雄)

    2 回复  |  直到 14 年前
        1
  •  28
  •   Pentium10    14 年前

    向清单文件添加权限

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

    编辑

    对于灯光,请尝试显式添加它们,默认灯光可能配置为nolight

    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0xff00ff00;
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;
    
        2
  •  15
  •   OneWorld    14 年前

    将设备置于睡眠状态,灯将亮起!;)