代码:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, tickertext, System.currentTimeMillis() + 10000);
// 后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字, 系统当前时间
notification.defaults = Notification.DEFAULT_ALL; // 声音与震动
notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击后自动消失
// 这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, main.class), 0);
// 点击通知后的动作,这里是转回main 这个Acticity
notification.setLatestEventInfo(this, title, content, pendingIntent);
notificationManager.notify(notification_id, notification);
其中,
//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
<uses-permission android:name="android.permission.VIBRATE" />