android 服务通知,从Android中的服务发送通知

从文档中可以看到这种类型的通知:

@java.lang.Deprecated

public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }

public Notification(android.os.Parcel parcel) { /* compiled code */ }

@java.lang.Deprecated

public void setLatestEventInfo(android.content.Context context, java.lang.CharSequence contentTitle, java.lang.CharSequence contentText, android.app.PendingIntent contentIntent) { /* compiled code */ }

更好的方法

您可以发送如下通知:

// prepare intent which is triggered if the

// notification is selected

Intent intent = new Intent(this, NotificationReceiver.class);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification

// the addAction re-use the same intent to keep the example short

Notification n = new Notification.Builder(this)

.setContentTitle("New mail from " + "test@gmail.com")

.setContentText("Subject")

.setSmallIcon(R.drawable.icon)

.setContentIntent(pIntent)

.setAutoCancel(true)

.addAction(R.drawable.icon, "Call", pIntent)

.addAction(R.drawable.icon, "More", pIntent)

.addAction(R.drawable.icon, "And more", pIntent).build();

NotificationManager notificationManager =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n);

最好的办法

上面的代码需要最低API级别11(Android 3.0)。

如果您的最低API级别低于11,那么您应该像这样使用支持库的NotificationCompat类。

因此,如果您的最低目标API级别为4+(Android 1.6+),请使用以下命令:

import android.support.v4.app.NotificationCompat;

-------------

NotificationCompat.Builder builder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.mylogo)

.setContentTitle("My Notification Title")

.setContentText("Something interesting happened");

int NOTIFICATION_ID = 12345;

Intent targetIntent = new Intent(this, MyFavoriteActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent);

NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

nManager.notify(NOTIFICATION_ID, builder.build());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值