6
A
回答
0
請設置通知方法
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
9
添加該代碼欲瞭解更多信息請參閱本Link。
添加權限到你的manifest文件
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
編輯 // 1.獲取一個參考NotificationManager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
// 2.實例化通知
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// 3. Defin E中的通知的擴展信息和意圖
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
// 4.通行證通知到NotificationManager
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
// ----------------- ----- //添加聲音 // ---------------------- // a。默認聲音
notification.defaults |= Notification.DEFAULT_SOUND;
// b。從SD卡自定義音效
notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");
// ---------------------- //添加振動 // ------ ---------------- // a。默認振動
notification.defaults |= Notification.DEFAULT_VIBRATE;
// b。自定義振動
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
// ------------------------ //添加閃燈 // ------ ------------------ // a。默認燈
notification.defaults |= Notification.DEFAULT_LIGHTS;
// b。自定義燈光
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
0
2
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_LIGHTS);
Intent notificationIntent = new Intent(context, MyActivity.class);
/* Set intent so it does not start a new activity */
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
/* Even if the mode is set to "Sound & Vibration" in the phone,
* the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
*/
switch (am.getRingerMode())
{
case AudioManager.RINGER_MODE_VIBRATE:
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
break;
case AudioManager.RINGER_MODE_NORMAL:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
break;
default:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
mBuilder.setContentIntent(intent);
notificationManager.notify(id, mBuilder.build());
相關問題
- 1. 打開/關閉閃光燈
- 2. 閃光燈未打開
- 3. Android開啓閃光燈LED?
- 4. 打開WP7中的相機閃光燈
- 5. 永久打開WP7閃光燈
- 6. 在Windows中打開背光燈CE
- 7. Windows Phone在後臺打開閃光燈
- 8. OpenGL:打開和關閉燈光
- 9. 使用Android camera2 API打開/關閉閃光燈不起作用
- 10. Android - RuntimeException嘗試打開閃光燈同時捕捉視頻
- 11. 當全屏閃光燈打開時Android WebView空白 - ICS 4.0
- 12. Android相機閃光燈在關閉後沒有打開
- 13. Windows 10手機應用程序打開閃光燈(燈)
- 14. 在打開的gl中繪製燈光時的凹凸燈
- 15. Android gcm通知打開片段
- 16. 打開閃光燈作爲黑莓上的光
- 17. 閃光燈爲Android
- 18. 用SL4A打開/關閉相機閃光燈蟒蛇Python
- 19. 如何打開和關閉設備閃光燈?
- 20. 手機閃光燈打開xamarin但它沒有打開,爲什麼?
- 21. 我如何能在Android的打開相機閃光燈的具體時間
- 22. 在Android中同時打開不支持閃光燈的手電筒和相機
- 23. Android NativeCamera打開與意圖與相機閃光燈關閉模式
- 24. 在Android中打開相機閃光LED?
- 25. 的Android - Camera2:打開電筒光線
- 26. 如何打開/關閉Swift 2 iPhone相機閃光燈?
- 27. 使用閃光燈按鈕打開SWFUpload對話框?
- 28. 在iPhone上同時打開閃光燈和前置攝像頭
- 29. 結構化文本PLC編程。打開和關閉燈光
- 30. 防止閃光燈打開新的窗口