android 组件Notification实例

本例是对Notification的练习,通过点击按钮弹出Notification系统通知。

主要知识点:

  • 获取系统通知管理器
private NotificationManager notificationManager;

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

  • 实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,"tickerText", System.currentTimeMillis());

函数解释:

android.app. Notification.Notification(int icon, CharSequence tickerText,long when)

Deprecated. Use Builder instead.

Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.

Parameters:

icon The resource id of the icon to put in the status bar.通知图标

tickerText The text that flows by in the status bar when the notification first activates.(看截图就明白了)

when The time to show in the time field. In the System.currentTimeMillis timebase.系统时间

  • 设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));

函数解释:

void android.app. Notification.setLatestEventInfo( Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

Deprecated. Use Builder instead.

Sets the contentView field to be a view with the standard "Latest Event" layout.

Uses the icon and when fields to set the icon and time fields in the view.

Parameters:

context The context for your application / activity.

contentTitle The title that goes in the expanded entry.

contentText The text that goes in the expanded entry.

contentIntent The intent to launch when the user clicks the expanded notification. If this is an activity, it must include theandroid.content.Intent.FLAG_ACTIVITY_NEW_TASK flag, which requires that you take care of task management as described in theTasks and Back Stack document.点击通知后触发的Intent,进入的activity。



 

布局文件:notification_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showNotification"
        android:text="弹出通知" />


</LinearLayout>

AtyNotification.java文件:

package com.fxj.composit;

 
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.view.View;


import com.fxj.compractice.R;


public class AtyNotification extends Activity {
private NotificationManager notificationManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
// 获取系统通知管理器
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 当再次点击系统通知进入此activity时取消通知。
if (notificationManager != null) {
notificationManager.cancel(R.layout.notification_layout);
}
}


@SuppressWarnings("deprecation")
public void showNotification(View view) {
// 实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,
"tickerText", System.currentTimeMillis());
// 设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));

               //执行Notification
notificationManager.notify(R.layout.notification_layout, notification);
}
}

运行效果:

结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子林Android

感谢老板,老板大气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值