简体   繁体   English

如何创建像whatsapp for android中使用的通知那样的弹出通知?

[英]How to create pop-up notifications like the notification used in whatsapp for android?

I want to create notification like the pop-up notification used in WhatsApp application for android devices.我想创建类似于 WhatsApp 应用程序中用于 android 设备的弹出通知的通知。

How can i do it?我该怎么做? Since i am a new user I cannot upload the screen shot由于我是新用户,我无法上传屏幕截图

please refer this link : http://cdn6.staztic.com/cdn/screenshot/appsdroidnotifythemeicecreamsandwich-1-1.jpg请参考此链接: http : //cdn6.staztic.com/cdn/screenshot/appsdroidnotifythemeicecreamsandwich-1-1.jpg

在此处输入图片说明

for the screen shot image and help me :)对于屏幕截图图像并帮助我:)

They're called 'heads-up' notifications.它们被称为“单挑”通知。 This page has a nice explanation. 这个页面有一个很好的解释。

To summarize, set the priority to High (or Max).总而言之,将优先级设置为高(或最大)。

Here is an example in my code:这是我的代码中的一个示例:

public static void notify(Context context, int id, int titleResId,
                          int textResId, PendingIntent intent) {
    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(titleResId);
    String text = context.getString(textResId);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle(title)
            .setContentText(text)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setWhen(System.currentTimeMillis())
            .setTicker(title)
            .setContentIntent(intent);
    notificationManager.notify(id, builder.build());
}

Declare a PopUpWindow reference and call initiatePopupWindow() method from where you want to open a pop up window:声明一个 PopUpWindow 引用并从要打开弹出窗口的位置调用initialPopupWindow() 方法:

private PopupWindow pwindo;

private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopUpWinndowDemoActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

} catch (Exception e) {
e.printStackTrace();
}
}

Have an screen_popup.xml for the pop up window like this:有一个 screen_popup.xml 用于弹出窗口,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />

</LinearLayout>

You can achieve it using Dialog theme or by making the activity's window transparent through setting window background to transparent in theme.您可以使用 Dialog 主题或通过在主题中将窗口背景设置为透明来使活动的窗口透明来实现它。 Just make your UI is in such a way that it looks like a Dialog.只需让您的 UI 看起来像一个对话框。

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

I don't know what whatsapp's pop-up looks like...我不知道 whatsapp 的弹出窗口是什么样的...

You can use a toast or a dialog您可以使用吐司对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM