android 通知Notification详解及实例代码

android 通知Notification详解及实例代码,第1张

概述androidNotification实例详解1.使用Builder模式来创建2.必须要设置一个smallIcon,还可以设置setTicker

androID Notification实例详解

1.使用Builder模式来创建

2.必须要设置一个smallicon,还可以设置setTicker

3.可以设置 setContentTitle,setContentInfo,setContentText,setWhen

4.可以设置setDefaults(闪屏,声音,震动),通过Notification设置flags(能不能被清除)

5.发送需要获取一个notificationmanager(getSystemService来获取);notify(int ID,Notification)

6.清除 manager.cancelAll(清除所有) cancal(int ID); 如果需要16一下的API也能访问,需要导入v4包,使用notificationCompat兼容类。

自定义通知

使用RemoteVIEws来建立一个布局,然后使用setContent()设置;

点击事件使用PendingIntent来完成

下面是一个案例

MainActivity类

public class MainActivity extends Activity {  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);  }  public voID clearNotification(VIEw v) {    // 通过代码来清除 NO_CLEAR    // 清除也需要manager    notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);    // 只能清除由本应用程序发出去的通知    manager.cancelAll();  }  public voID sendNotification(VIEw v) {    // 他是 在v4包中的通知,用于16以下版本发送通知    // NotificationCompat    // 通知的创建    Notification.Builder builder = new Notification.Builder(this);    // NotificationCompat.Builder builder2 = new NotificationCompat.Builder(    // this);    // 显示在通知条上的图标 必须的    builder.setSmallicon(R.drawable.ic_launcher);    // 显示通知条上的文字 不是必须的    builder.setTicker("您有一条新的消息!!");    // 状态栏中的    builder.setContentTitle("大标题");    builder.setContentText("文本");    builder.setWhen(System.currentTimeMillis());    builder.setContentInfo("Info");    builder.setDefaults(Notification.DEFAulT_ALL);    // 点击事件使用Pending    Intent intent = new Intent(this,MainActivity.class);    PendingIntent pi = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);    builder.setContentIntent(pi);    // 生成对象 16API以上,支持低版本需要使用v4包中的notificationCompat    Notification notify = builder.build();    // 设置不能清除    notify.flags = Notification.FLAG_NO_CLEAR;    // 如何将通知发送出去    notificationmanager mananger = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);    // 通知的唯一值,如果ID重复,表明是更新一条通知,而不是新建    mananger.notify((int) (Math.random() * 1000),notify);  }  public voID diyNotification(VIEw v) {    // 展示在通知上面的视图    RemoteVIEws vIEws = new RemoteVIEws(getPackagename(),R.layout.layout);    Notification notification = new Notification.Builder(this)        .setSmallicon(R.drawable.ic_launcher).setTicker("自定义通知 ")        // 布局        .setContent(vIEws).build();    // 使用RemoteVIEws来设置点击事件    vIEws.setTextcolor(R.ID.tv,color.RED);    Intent intent = new Intent(this,OneActivity.class);    // 音乐播放是放在Service    PendingIntent pi = PendingIntent.getActivity(this,2,PendingIntent.FLAG_UPDATE_CURRENT);    vIEws.setonClickPendingIntent(R.ID.tv,pi);    Intent intent2 = new Intent(this,MainActivity.class);    PendingIntent pi2 = PendingIntent.getActivity(this,intent2,PendingIntent.FLAG_UPDATE_CURRENT);    vIEws.setonClickPendingIntent(R.ID.iv,pi2);    // 发送    notificationmanager notify = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);    notify.notify(1,notification);  }}

OneActivity类

public class OneActivity extends Activity {  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    TextVIEw tv = new TextVIEw(this);    tv.setText("跳转界面");    setContentVIEw(tv);  }}

activity.main.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:orIEntation="vertical"  tools:context="com.example.lesson8_notification.MainActivity" >  <button    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:onClick="sendNotification"    androID:text="普通的通知" />  <button    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:onClick="clearNotification"    androID:text="清除所有通知" />  <button    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:onClick="diyNotification"    androID:text="自定义通知" /></linearLayout>

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" >  <TextVIEw    androID:ID="@+ID/tv"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:text="TextVIEw" />  <ImageVIEw    androID:ID="@+ID/iv"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:src="@drawable/ic_launcher" /></linearLayout>

最后记得注册activity

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的android 通知Notification详解及实例代码全部内容,希望文章能够帮你解决android 通知Notification详解及实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147407.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存