Android中新增常駐通知欄
直接看程式碼吧,很簡單的小功能,核心程式碼就是設定notification的flags為Notification.FLAG_ONGOING_EVENT。
// 新增常駐通知
private void setNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(this, Rcp_birthdayActivity.class);
notification.flags = Notification.FLAG_ONGOING_EVENT; // 設定常駐 Flag
PendingIntent contextIntent = PendingIntent.getActivity(this, 0,
intent, 0);
notification.setLatestEventInfo(getApplicationContext(),
getString(R.string.app_name), "點選檢視", contextIntent);
notificationManager.notify(R.string.app_name, notification);
}
// 取消通知
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(R.string.app_name);
}