首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android系统中的新闻发布

问Android系统中的新闻发布
EN

Stack Overflow用户
提问于 2013-10-14 07:07:25
回答 1查看 625关注 0票数 0

我正在开发应用程序,在这个应用程序中我需要实现新闻通知的一个功能。

例如,NDTV新闻应用程序:每当有新消息时,它就会发出通知。这是怎么发生的?

有人能给我举个解决方案的例子吗?

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2013-10-17 09:36:28

使用以下代码:

代码语言:javascript
复制
    private void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, myactivity.class), 0);

    // To support 2.3 os, we use "Notification" class and 3.0+ os will use
    // "NotificationCompat.Builder" class.
    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification(icon, message, 0);
        notification.setLatestEventInfo(context, appname, message,
                contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);

    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(0)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when , notification);

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19355059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档