发布于 2017-10-10 12:54:08 | 178 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了android 通知Notification详解及实例代码的相关资料,需要的朋友可以参考下

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, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.tv, pi);

    Intent intent2 = new Intent(this, MainActivity.class);

    PendingIntent pi2 = PendingIntent.getActivity(this, 1, 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

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务