试图建立在Android中一个简单的通知 [英] Trying to build a simple notification in android

查看:210
本文介绍了试图建立在Android中一个简单的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置我的Andr​​oid应用程序的通知,将简单地说它的工作,但我需要我的应用程序有兼容下降到API 1.一路我对如何做到这一点真的很困惑虽然。有老教程被取消precated,并有不支持旧的API级别的新教程。根据这一 SO 的问题,我应该使用的 NotificationCompat.Builder 。还有就是我用一个例子,但我不完全了解code。

I'm trying to set a notification in my android app that will simply say "It worked", but I need my app to have compatibility all the way down to API 1. I'm really confused on how to do this though. There are old tutorials that are deprecated, and there are new tutorials that don't support older API levels. According to this SO question, I should use NotificationCompat.Builder. There is an example that I'm using, but I don't fully understand the code.

出于这种code的:

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

我得到的红线下: CTX YO​​UR_PI_REQ_ code YOUR_NOTIF_ID

推荐答案

CTX 变量是打算成为一个Android上下文 - 通常是一个活动(或实际上是一个类扩展活动)。

The ctx variable is intended to be an Android context -- often an Activity (or actually a class that extends Activity).

您应该做的的PendingIntent 类一点点研究,以了解 YO​​UR_PI_REQ_ code ,但你需要决定放什么位置;它的的挂起的意图请求code。

You should do a little research on the PendingIntent class to understand YOUR_PI_REQ_CODE but you need to determine what to put here; it's your pending intent request code.

您也应该研究的 NotificationManager 通知()的方法来确定你想要的的使用什么您的通知ID。

You should also research the NotificationManager notify() method to determine what you want to use as your notification ID.

这篇关于试图建立在Android中一个简单的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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