您的位置:首页 > 其它

andriid:通知的简单记录(11)

2016-01-23 00:00 246 查看
摘要: 通知就是像短信来的时候有提醒://要发送通知,首先 获取通知管理器,NotificationManager notifyManager= (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);

package com.example.andday11notifiation;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
//通知的简单记录,四个按钮事件代码

public class MainActivity extends Activity {
private NotificationManager notifyManager;// 通知管理器

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得通知管理器
notifyManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
}

// 简单文本的通知
public void notifySimpleText(View view) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
// 设置提示图标,标题,内容,循环滚动的内容
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("提示信息")
.setContentText("注意:今晚气温变冷,请多加衣物!!")
.setTicker("注意:今晚气温变冷,请多加衣物!!注意:今晚气温变冷,请多加衣物!!")
// 将信息滚动显示
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
// 默认提醒方式声音和震动,要加权限
.setContentIntent(
PendingIntent.getActivity(this, 100, new Intent(
Intent.ACTION_CALL, Uri.parse("tel:10086")),
PendingIntent.FLAG_ONE_SHOT));// 点击通知可以打电话,记得加权限
/*
* setContentIntent(PendingIntent.getActivity(context, requestCode,
* intent, flags)) context:上下文 requestCode:请求码
* intent:一个延时意图,即当点击的通知时候才跳转,flags:当通知来时只能点击一次这个通知
*/
notifyManager.notify(1, builder.build());// 通知管理进行通知管理动作,启动发送通知
}

// 进度的通知
public void notifyProgress(View view) {
new Thread(new Runnable() {
@Override
public void run() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this);
// 设置提示图标、标题,内容,循环滚动的内容
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("有新通知").setContentText("正在下载!")
.setTicker("正在下载!正在下载!正在下载!");
for (int i = 0; i <= 100; i++) {
// builder.setProgress(max, progress, indeterminate)
builder.setProgress(100, i, false);// indeterminate进度条样式:一把用false
notifyManager.notify(2, builder.build());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 下载完后通知跟新:提示下载完
notifyManager.cancel(2);// 下载完取消
/*
* NotificationCompat.Builder builder2 = new
* NotificationCompat.Builder( MainActivity.this);
* //可以重复利用上面的builder,没必要再新创建一个
*/
builder = new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("提示消息").setContentText("下载完毕");
notifyManager.notify(3, builder.build());
}
}).start();
}

// 列表通知,即显示多列表个选项
public void notifyList(View view) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息");
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
// 上面new一个通知样式
style.addLine("列表1:你好");
style.addLine("列表2:嘿嘿嘿");
style.addLine("列表3:区34213123");
builder.setStyle(style);// 加进样式
notifyManager.notify(4, builder.build());
}

// 大图标通知
public void notifyBigImage(View view) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息");
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
// 设置一个通知样式
style.bigLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.notify_newmessage));
style.bigPicture(BitmapFactory.decodeResource(getResources(),
R.drawable.mm));// 通知的大图片
builder.setStyle(style);
notifyManager.notify(5, builder.build());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: