2011-09-16 100 views
19

我一直在處理推送通知,並且能夠實施它並將其顯示在狀態欄上,所面臨的問題是即使手機處於鎖定狀態,也要顯示它在它所說的鎖屏(「拖動解鎖」)下,我看到類似這樣的通知,但無法找到任何示例。Android如何在屏幕上顯示通知

例如: 就像您收到未接來電時,它會顯示在屏幕上的鎖定按鈕下。

代碼:

String ns = Context.NOTIFICATION_SERVICE; 
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
int icon = R.drawable.icon_launcher; 
CharSequence tickerText = "MyApplication"; 
long when = System.currentTimeMillis(); 
Notification notification = new Notification(icon, tickerText, when); 
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;; 
CharSequence contentTitle = this.title; 
CharSequence contentText = this.message; 
Intent notificationIntent = new Intent(this, MainActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
mNotificationManager.notify(NOTICE_ID, notification); 
+2

無關,你不一定需要保存一切,局部變量 – CrackerJack9

+2

這現在是不可能的。 –

+0

@Copa你是什麼意思?你有什麼參考?我有幾個應用程序以OP描述的方式顯示通知(通知欄,屏幕鎖定)。 – CrackerJack9

回答

3

您是否嘗試過用標誌創建alertdialog?該flag_show_when_locked應該做的伎倆。 請參考此主題,您應該在這裏找到更詳細的答案。 Android Lock Screen Widget

11

使用NotificationCompat.Builder

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) // notification icon 
      .setContentTitle("Notification!") // title for notification 
      .setContentText("Hello word") // message for notification 
      .setAutoCancel(true); // clear notification after click 
Intent intent = new Intent(this, MainActivity.class); 
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK); 
mBuilder.setContentIntent(pi); 
NotificationManager mNotificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(0, mBuilder.build()); 

推送通知在鎖定屏幕創建通知 http://www.hongkiat.com/blog/android-lock-screen-notifications/

+3

這將不會幫助,實際上顯示設備的鎖定屏幕上的通知會嗎? –

+1

是的,即使設備被鎖定也會顯示通知 – Naruto

+2

Lint表示FLAG_ACTIVITY_NEW_TASK與PendingIntent – auval

2

你所看到的,實際上可能是放置在一個自定義的widget主機鎖屏小部件的通知。

如果您針對InstallWidgetReceiver Android平臺的源代碼看起來遲4.4.3這裏:

https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java

您將通過筆者看到這樣一個字條:

/** *我們稍後可能會對此進行補充,以處理允許外部應用程序放置小部件,但現在,我們只是想公開其他方面的操作。 */

而且你可以看到,InstallWidgetReceiver.java其實不是由谷歌以同樣的方式充實爲InstallShortCutReceiver.java是。所以看起來至少達到4.4.3你不能像使用InstallShortCutReceiver爲主屏幕添加快捷方式那樣添加小部件到本地鎖定屏幕。

除非您將自己的鎖屏應用程序構建爲窗口小部件主機,並且用戶代替本地進行安裝,否則使用窗口小部件可能會不太順利。

但是,另一種方法就是設置getWindow()。addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

這將顯示您的活動,無論屏幕是否鎖定。屏幕鎖定時關閉此活動將顯示鎖定的屏幕。

2

創建通知使用NotificationCompat.Builder但一定要放的知名度公像

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder 
     .setContentTitle("Title") 
     .setContentText("content") 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen