如何在Android中显示顶部栏消息

时间:2014-10-28 10:39:54

标签: android android-layout notifications

我希望在完成一些后台工作后在Android顶部显示此类消息。 如何显示此类通知? 感谢..

enter image description here

4 个答案:

答案 0 :(得分:4)

这看起来像Notification的自动收录器文本,假设此屏幕截图位于屏幕顶部。您可以通过setTicker() on your NotificationCompat.Builder将其添加到Notification。但请注意,自API级别21(Android 5.0)起不再显示自动收录器文本。另请注意,滚动条文本会在几秒钟后自动消失。

答案 1 :(得分:2)

这是一个通知警报,可以从服务,活动,片段,广播接收器等开始... 请检查此链接:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

此示例是从google dev复制的:

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());
祝你好运!

答案 2 :(得分:1)

构建通知并使用setTicker(查看文档there):

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

答案 3 :(得分:0)

您可以使用SYSTEM_ALERT_WINDOW,无论您的应用程序是前台还是后台,都会在Top上显示消息。

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" />