我的代码有什么问题 - 通知 - 没有声音没有震动
问题描述:
我似乎有我的代码有问题。 我已经创建了一个测试活动,只是为了看看有什么问题,我仍然不能。我的代码有什么问题 - 通知 - 没有声音没有震动
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;
答
对于所有默认值(声音,振动&光)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" />
设置震动权限