Notificatin 使用

Notificatin

[功能]
左上角的那个信息提示 可以弹出一些信息 比如 状态更新 或 其他
[img]http://dl.iteye.com/upload/picture/pic/52746/118cbfed-14b7-3da8-a4e4-3115fb11e9ff.png[/img]

[思路]
1. 得到NotificationManager 用于把Notification抛出
2. 构造一个Notification 设定与Notification相关的信息 包括2个方面:左上角提示信息 和 Expanded 信息

[实现]
1. 得到NotificationManager的实例 用于抛出Notification

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


2. 构造一个Notification 并传入一些信息 比如 图标 文字 弹出时间

int icon = R.drawable.icon;
CharSequence tickerText = "HelloNotfication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);;
notification.defaults=Notification.DEFAULT_SOUND;


3. 设定Notification的expand 信息 可通过 HOMR->MENU->Notification 查看
[img]http://dl.iteye.com/upload/picture/pic/52748/ce3a5804-f120-393d-86f0-40489b75b971.png[/img]

//to definition some vale in Expanded view
CharSequence contentTitle = "Notification's title @ Expanded view"; // expanded message title
CharSequence contentText = "Notification's text @ Expanded view"; // expanded message text
//to definition an Intent that this Notification listener in Expanded view via clicked action
Intent notificationIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://110"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);


4. 抛出Notification.

manager.notify(1, notification);


5. 定制Expand 信息也是可以的
× 定制目标的 custom_notification_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>
</LinearLayout>


× 设定一些使用信息 比如 文章内容

// to customize its Expanded view via XML file
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.msn);

// to specific the text in Expanded view
contentView.setTextViewText(R.id.text, "Hello, this is Expanded view with custom XML");

notification.contentView = contentView;



其他信息稍后补充 比如 Notification弹出的地方 Expand地方!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值