安卓学习UI组件-Notifications-通知栏-四种案例

安卓学习UI组件-Notifications-通知栏-四种案例_第1张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第2张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第3张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第4张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第5张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第6张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第7张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第8张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第9张图片

安卓学习UI组件-Notifications-通知栏-四种案例_第10张图片

案例:

安卓学习UI组件-Notifications-通知栏-四种案例_第11张图片

普通视图的通知,使用了Intent传递信息,点击通知,打开一个Activity

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.myapplication.MainActivity">

<Button
android:id="@+id/button_normal_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:onClick="sendNotification1"
android:text="发送一个普通视图的通知" />

<Button
android:id="@+id/button_big_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button_normal_view"
android:onClick="sendNotification2"
android:text="发送一个大视图的通知" />

<Button
android:id="@+id/button_progres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button_big_view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_big_view"
android:layout_below="@+id/button_big_view"
android:onClick="sendNotification3"
android:text="发送一个进度条通知" />

<Button
android:id="@+id/button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button_progres"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_progres"
android:layout_below="@+id/button_progres"
android:onClick="sendNotification4"
android:text="发送一个自定义视图的通知" />
</RelativeLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.myapplication.MainActivity2">

<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="妈的智障!" />
</RelativeLayout>

MainActivity2.java

package com.example.administrator.myapplication;

import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class MainActivity2 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//打开界面后取消指定ID通知
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(MainActivity.NID_1);
        String msg = getIntent().getStringExtra("msg");
TextView tv = (TextView) findViewById(R.id.msg);
tv.setText(msg);
}

}

自定义视图布局文件custom_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="wrap_content"
android:gravity="center"
android:orientation="horizontal">


<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="64dp"
android:maxWidth="64dp"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/textView_song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="歌曲名称。。。" />

<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />

<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一首" />
</LinearLayout>

MainActivity.java

package com.example.administrator.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.RemoteViews;

//通知:Notifications
public class MainActivity extends ActionBarActivity {


public static final int NID_1 = 0x1;
public static final int NID_2 = 0x2;
public static final int NID_3 = 0x3;
public static final int NID_4 = 0x4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//发送一个普通的通知
public void sendNotification1(View v) {
// Notification n=new Notification(); API11之前创建通知的方式,被弃用
//API11之后使用
// Notification.Builder builder=new Notification.Builder(this);
//v4支持包
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置相关的属性
builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
builder.setContentTitle("你有一条新消息");
builder.setContentText("龙族还未更新,敬请期待,好好学习,天天向上,2016更任性");
// builder1.setOngoing(true);//设置为常驻通知 常驻通知不可清楚掉
// builder1.setAutoCancel(true);//自动清除通知
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setNumber(10);
builder.setTicker("新消息");

//定义一个意图,当点击通知时要打开一个界面(Activity)
Intent intent = new Intent(this, MainActivity2.class);

intent.putExtra("msg", "龙族 ");
//参数(上下文,请求编码(没用),意图,创建PendingIntent的方式)
/* PendingIntent.FLAG_CANCEL_CURRENT; 取消当前的pi,创建新的
PendingIntent.FLAG_NO_CREATE; 如果有就使用,没有就不创建
PendingIntent.FLAG_ONE_SHOT;只是用一次
PendingIntent.FLAG_UPDATE_CURRENT;如果有,更新Intent,在使用,没有就创建*/
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//设置通知的事件
builder.setContentIntent(pi);


//根据设置好的属性 创建通知对象
Notification n = builder.build();
//获取系统的通知管理器 然后发送通知
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_1, n);

}

public void sendNotification2(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置相关的属性
builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
/* builder.setContentTitle("新消息");
builder.setContentText("太乙仙魔录之灵山纪");*/
builder.setTicker("有消息");
builder.setDefaults(Notification.DEFAULT_ALL);


//设置大视图的样式
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle("雁城雪");

style.addLine("十步杀一人,千里不留名。");
style.addLine("事了拂衣去,深藏身与名。");
style.addLine("一行白鹭上青天。");
style.setSummaryText("作者:小肥");

builder.setNumber(5);
builder.setStyle(style);

Notification n = builder.build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_2, n);


// builder.setStyle();
}


//显示进度条的通知
public void sendNotification3(View v) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置相关的属性
builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
builder.setContentTitle("更新中。。。");
builder.setContentText("正在由装逼模式更新至牛逼模式");
builder.setTicker("有消息");
// builder.setDefaults(Notification.DEFAULT_ALL);
builder.setProgress(100, 5, false);//false代表确定的进度
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//根据设置好的属性 创建通知对象
Notification n = builder.build();
//获取系统的通知管理器 然后发送通知
nm.notify(NID_3, n);

//模拟更新的线程

new Thread(new Runnable() {
@Override
public void run() {

for (int progress = 0; progress <= 100; progress += 5) {
builder.setProgress(100, progress, false);
nm.notify(NID_3, builder.build());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setProgress(0, 0, true);
// builder.setProgress(0, 0, false);
builder.setContentText("更新完成,由于机主逼格不够,正在返回逗比模式");
nm.notify(NID_3, builder.build());
}
}).start();
}


public void sendNotification4(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置相关的属性
builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标
builder.setOngoing(true);
/* builder.setContentTitle("更新中。。。");
builder.setContentText("正在由装逼模式更新至牛逼模式");
builder.setTicker("有消息");*/
//创建一个远程的视图
RemoteViews views = new RemoteViews(getPackageName(), R.layout.custom_layout);
views.setTextViewText(R.id.textView_song_name, "第一滴泪");
// views.setImageViewResource();
views.setTextViewText(R.id.button_play, "暂停");

// views.setOnClickPendingIntent();用来设置按钮的单击事件,启动服务


builder.setContent(views);
builder.setTicker("小肥音乐");

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//根据设置好的属性 创建通知对象
Notification n = builder.build();
//获取系统的通知管理器 然后发送通知
nm.notify(NID_4, n);


}
}

你可能感兴趣的:(安卓学习UI组件-Notifications-通知栏-四种案例)