如何在Android的通知? [英] How to create notifications in Android?

查看:108
本文介绍了如何在Android的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    int icon = R.drawable.icon4;        
    CharSequence tickerText = "Hello"; // ticker-text
    long when = System.currentTimeMillis();         
    Context context = getApplicationContext();     
    CharSequence contentTitle = "Hello";  
    CharSequence contentText = "Hello";      
    Intent notificationIntent = new Intent(this, Example.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

对我来说这是行不通的。任何帮助吗?我以前用的东西和它的工作,但我的项目删除了。 :(

This won't work for me. Any help? I used something before and it worked, but my project got deleted. :(

所以,回到我的主要问题,我怎么可以创建一个通知,是可以点击的,去我的应用程序,但不会消失单击时?

So back to my main question, how could I create a notification that is clickable and goes to my app, but does not go away when clicked?

推荐答案

您应该阅读整个事情不只是一个组成部分,哥们。请仔细重读一步一步。

You should read the whole things not just a part, buddy. Please re-read carefully step-by-step.

// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.icon4;        
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicationContext();     
CharSequence contentTitle = "Hello";  
CharSequence contentText = "Hello";      
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// and this
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);

这篇关于如何在Android的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆