2014-10-28 67 views
4

我想在完成一些後臺工作後在Android頂部顯示這種消息。 如何顯示此類通知? 謝謝..如何在Android中顯示頂部欄消息

enter image description here

+0

使用自定義的吐司,請參考這個http://stackoverflow.com/questions/11288475/custom-toast-in-android-a-simple-example – 2014-10-28 10:42:22

+0

什麼阻止你使用TextView對齊父對象的頂部,左邊複合可繪製在裏面?通常它會是INVISIBLE,然後你只有在需要的時候才能看到它。 – 2014-10-28 10:47:57

回答

4

這看起來像一個Notification的股票文本,假定該截圖是在屏幕的頂部。您可以通過setTicker() on your NotificationCompat.Builder將其添加到Notification。但是,請注意,從API Level 21(Android 5.0)開始,不再顯示股票行情文本。另外請注意,幾秒鐘後,股票行情文本會自動消失。

+0

是的,setTicker()是方法,它的工作原理。謝謝 – 2014-10-28 14:48:58

0

您可以使用SYSTEM_ALERT_WINDOW,它始終在上面顯示消息,無論您的應用程序是前臺還是後臺。

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); 

params.gravity = Gravity.CENTER|Gravity.TOP; 

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
wm.addView(view, params); 





<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
1

建設的通知,並使用setTicker(看DOC there):

NotificationCompat.Builder notificationBuilder = 
     new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_event) 
     .setContentTitle(eventTitle) 
     .setContentText(eventLocation) 
     .setContentIntent(viewPendingIntent) 
     .setTicker("Your message"); 
2

這是通知警告,這可以從服務,活動,碎片,廣播接收器等開始。 .. 檢查此鏈接:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

這個例子是從谷歌開發複製:

NotificationCompat.Builder mBuilder = 
    new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.notification_icon) 
    .setContentTitle("My notification") 
    .setContentText("Hello World!"); 
Intent resultIntent = new Intent(this, ResultActivity.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
stackBuilder.addParentStack(ResultActivity.class); 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = 
    stackBuilder.getPendingIntent(
     0, 
     PendingIntent.FLAG_UPDATE_CURRENT 
    ); 
mBuilder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(mId, mBuilder.build()); 

祝你好運!

相關問題