您的位置:首页 > 产品设计 > UI/UE

Android开发之使用Notification.Builder

2015-05-17 03:03 393 查看
通知的主要功能是提示功能。例如:短信、推送信息等等。

大体使用步骤:

1.获取状态通知栏管理

NotificationManager 是一个系统Service,所以必须通过
getSystemService(NOTIFICATION_SERVICE)方法来获取。




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


2.实例化通知栏构造器NotificationCompat.Builder

3.设置NotificationCompat.Builder

4.设置PendingIntent

5.显示

方法或参数介绍:

1.[b]PendingIntent[/b]

PendingIntent.getBroadcast(context, requestCode, intent, flags)



PendingIntent.getActivities(context, requestCode, intents, flags)



PendingIntent.getService(context, requestCode, intent, flags)


中的flags属性参数:

FLAG_ONE_SHOT 表示返回的PendingIntent仅能执行一次,执行完后自动取消

FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL

FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent

FLAG_UPDATE_CURRENT 表示更新的PendingIntent

2.notification.flags参数介绍

Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符

Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)

Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)

Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次

Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失

Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)

Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务

使用方法:

在设置完属性后,设置
Notification notification =builder.build();
notification.flags =Notification.FLAG_ONLY_ALERT_ONCE;


3.setVibrate(long[] pattern)

设置震动,需要权限.
<uses-permission android:name="android.permission.VIBRATE"/>


4.builder.setOngoing( )

设置为ture,表示它为一个正在进行的通知。简单的说,当为ture时,不可以被侧滑消失。

***************************************************************************************

使用自定义Notification,就要使用RemoteViews。

***************************************************************************************

使用实例:

图片:







实现代码:

MainActivity.java
public class MainActivity extends Activity {

Button button, button2;
NotificationManager notificationManager;
public final static String NEWS_LISTEN = "broadcast";

// 用于自定义Notification,点击事件的验证
String remoteViewsText = "未点击";

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

IntentFilter filter = new IntentFilter();
filter.addAction(NEWS_LISTEN);
this.registerReceiver(broadcastReceiver, filter);

}

public void click(View v) {

switch (v.getId()) {
case R.id.but:// 使用普通的Notification

Notification.Builder builder = new Notification.Builder(
MainActivity.this);

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.close);// 设置图标
builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
// builder.setAutoCancel(true);
builder.setContentTitle("标题");// 设置通知的标题
builder.setContentText("内容");// 设置通知的内容
builder.setTicker("状态栏上显示");// 状态栏上显示
builder.setOngoing(true);

/*
* // 设置声音(手机中的音频文件) String path =
* Environment.getExternalStorageDirectory() .getAbsolutePath() +
* "/Music/a.mp3"; File file = new File(path);
* builder.setSound(Uri.fromFile(file));
*/

// 获取Android多媒体库内的铃声
builder.setSound(Uri.withAppendedPath(
Audio.Media.INTERNAL_CONTENT_URI, "5"));

// builder.setVibrate(new long[]{2000,1000,4000}); //需要真机测试
Notification notification = builder.build();
// notification.flags =Notification.FLAG_ONGOING_EVENT;

notificationManager.notify(0, notification);

break;

case R.id.but2:// 使用自定义的Notification
// 3.0之前不支持Button
MyNotification();

break;

case R.id.but3:// 使用下载的Notification,在4.0以后才能使用

final Notification.Builder builder3 = new Notification.Builder(
MainActivity.this);
builder3.setSmallIcon(R.drawable.ic_launcher)
.setTicker("showProgressBar").setContentInfo("contentInfo")
.setOngoing(true).setContentTitle("ContentTitle")
.setContentText("ContentText");
// 模拟下载过程
new Thread(new Runnable() {
@Override
public void run() {

int progress = 0;

for (progress = 0; progress < 100; progress += 5) {
// 将setProgress的第三个参数设为true即可显示为无明确进度的进度条样式
builder3.setProgress(100, progress, false);

notificationManager.notify(0, builder3.build());
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
System.out.println("sleep failure");
}
}
builder3.setContentTitle("Download complete")
.setProgress(0, 0, false).setOngoing(false);
notificationManager.notify(0, builder3.build());

}
}).start();

break;
case R.id.but4:// 大布局通知在4.1以后才能使用,BigTextStyle

Notification.BigTextStyle textStyle = new Notification.BigTextStyle();
textStyle.setBigContentTitle("大标题")
// 标题
.setSummaryText("SummaryText")
.bigText(
"Big Text!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
+ "!!!!!!!!!!!"
+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");// 内容
Notification.Builder builder2 = new Notification.Builder(
MainActivity.this);
builder2.setSmallIcon(R.drawable.icon);// 小图标
// 大图标
builder2.setLargeIcon(BitmapFactory.decodeResource(
this.getResources(), R.drawable.close));
builder2.setTicker("showBigView_Text")
.setContentInfo("contentInfo");
builder2.setStyle(textStyle);
builder2.setAutoCancel(true);

notificationManager.notify(0, builder2.build());

break;

case R.id.but5://大布局通知在4.1以后才能使用,大布局图片

Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.back));

Notification.Builder builder4 = new Notification.Builder(
MainActivity.this);
builder4.setSmallIcon(R.drawable.icon);// 小图标
// 大图标
builder4.setLargeIcon(BitmapFactory.decodeResource(
this.getResources(), R.drawable.close));
builder4.setTicker("showBigView_Picture")
.setContentInfo("contentInfo");
builder4.setStyle(bigPictureStyle);
builder4.setAutoCancel(true);

notificationManager.notify(0, builder4.build());

break;

case R.id.but6://大布局通知在4.1以后才能使用,InboxStyle

Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("InboxStyle");
inboxStyle.setSummaryText("Test");
for(int i =0 ;i<5;i++){
inboxStyle.addLine("new:"+i);
}

Notification.Builder builder5 = new Notification.Builder(
MainActivity.this);
builder5.setSmallIcon(R.drawable.icon);// 小图标
// 大图标
builder5.setLargeIcon(BitmapFactory.decodeResource(
this.getResources(), R.drawable.close));
builder5.setTicker("showBigView_InboxStyle")
.setContentInfo("contentInfo");
builder5.setStyle(inboxStyle);
builder5.setAutoCancel(true);

notificationManager.notify(0, builder5.build());

break;

}

}

@Override
protected void onDestroy() {
super.onDestroy();
// 取消广播接收
this.unregisterReceiver(broadcastReceiver);
}

/**
* 自定义Notification
*/
public void MyNotification() {

RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.form);
remoteViews.setTextViewText(R.id.tv_form, remoteViewsText);

Intent intent2 = new Intent(MainActivity.NEWS_LISTEN);

// 使用广播,所以INTENT必须用getBroadcast方法
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(
MainActivity.this, 1, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);

// 绑定
remoteViews.setOnClickPendingIntent(R.id.but_form, pendingIntent2);

Notification.Builder builderMain = new Notification.Builder(
MainActivity.this);

builderMain
.setContent(remoteViews)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(
BitmapFactory.decodeResource(this.getResources(),
R.drawable.open)).setOngoing(true)
.setTicker("music is playing");
notificationManager.notify(0, builderMain.build());
}

// 广播接收器(自定义Notification使用到)
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context c, Intent intent) {

if (intent.getAction().equals(NEWS_LISTEN)) {

remoteViewsText = "已点击";
MyNotification();

}
}
};
}


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="${relativePackage}.${activityClass}" >

<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification one" />

<Button
android:id="@+id/but2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification two" />

<Button
android:id="@+id/but3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification three" />

<Button
android:id="@+id/but4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification four" />

<Button
android:id="@+id/but5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification five" />

<Button
android:id="@+id/but6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="notification six" />

</LinearLayout>


form.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/but_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="嘻嘻" />

<TextView
android:id="@+id/tv_form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="无"/>

</LinearLayout>


SecondActivity.java 只是一个activity。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: