Notification的基本使用
2017-02-09 16:12
169 查看
// 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
以一个为例
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) //icon的图片设置
.setTicker("TickerText:" + "您有新短消息,请注意查收!")//提醒的类容
.setContentTitle("Notification Title")//提醒的标题
.setContentText("This is the notification message")/提醒类容
.setContentIntent(pendingIntent3).setNumber(1).build();
PendingIntent//跳转界面 传递信息用的
示例
intent1 = new Intent(getApplicationContext(),
ContentActivity.class);
intent1.putExtra("content", "444");
intent1.putExtra("number", "444");
intent1.putExtra("date", "444");
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent有一个getActivity方法,第一个参数是上下文,第二个参数 requestCode,第三个参数是 Intent,用来存储信息,第四个参数是对参数的操作标识,常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。
关于第二个参数当requestCode值一样时,后面的就会对之前的消息起作用,所以为了避免影响之前的消息,requestCode每次要设置不同的内容。
myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
myNotify.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
这个设置滚动条
可以写带滚动条的提示
manager.notify(NOTIFICATION_FLAG, notify2);中的NOTIFICATION_FLAG是int类型是提示信息的表示重复不会弹出显示
贴出全部代码有的
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
以一个为例
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) //icon的图片设置
.setTicker("TickerText:" + "您有新短消息,请注意查收!")//提醒的类容
.setContentTitle("Notification Title")//提醒的标题
.setContentText("This is the notification message")/提醒类容
.setContentIntent(pendingIntent3).setNumber(1).build();
PendingIntent//跳转界面 传递信息用的
示例
intent1 = new Intent(getApplicationContext(),
ContentActivity.class);
intent1.putExtra("content", "444");
intent1.putExtra("number", "444");
intent1.putExtra("date", "444");
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent有一个getActivity方法,第一个参数是上下文,第二个参数 requestCode,第三个参数是 Intent,用来存储信息,第四个参数是对参数的操作标识,常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。
关于第二个参数当requestCode值一样时,后面的就会对之前的消息起作用,所以为了避免影响之前的消息,requestCode每次要设置不同的内容。
myNotify = new Notification();
myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
myNotify.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
notifyBuilder.setProgress(100, incr, false);
这个设置滚动条
可以写带滚动条的提示
/** * 带滚动条的提示 */ public void Pro_Notification() { manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notifyBuilder = new NotificationCompat.Builder(this); notifyBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setTicker("TickerText:您有新下载消息,请注意查收!") .setOngoing(true) .setSmallIcon(R.drawable.ic_launcher); // Start a lengthy operation in a background thread new Thread( new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr += 5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state notifyBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. manager_n.notify(0, notifyBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep( 1000); } catch (InterruptedException e) { Log.d("NOTIFICATION", "sleep failure"); } } // When the loop is finished, updates the notification notifyBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0, 0, false); manager_n.notify(213, notifyBuilder.build()); } } // Starts the thread by calling the run() method in its Runnable ).start(); }
manager.notify(NOTIFICATION_FLAG, notify2);中的NOTIFICATION_FLAG是int类型是提示信息的表示重复不会弹出显示
贴出全部代码有的
package com.yundong.pushmessage;
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.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
private static final int NOTIFICATION_FLAG = 1;
private static final int NOTIFICATION_FLAG_4 = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationMethod(View view) {
Notification myNotify;
RemoteViews rv;
Intent intent1;
PendingIntent contentIntent;
// 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
switch (view.getId()) {
// 默认通知
case R.id.btn1:
// Notification myNotify = new Notification(R.drawable.message,
// "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());
myNotify = new Notification();
myNotify.icon = R.drawable.ic_launcher;
myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
myNotify.when = System.currentTimeMillis();
myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
myNotify.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
rv = new RemoteViews(getPackageName(),
R.layout.my_notification);
rv.setTextViewText(R.id.text_content, "hello wrold!");
myNotify.contentView = rv;
// Intent intent = new Intent(Intent.ACTION_MAIN);
intent1 = new Intent(getApplicationContext(),
ContentActivity.class);
intent1.putExtra("content", "111");
intent1.putExtra("number", "111");
intent1.putExtra("date", "111");
contentIntent = PendingIntent.getActivity(this, 1,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
myNotify.contentIntent = contentIntent;
manager.notify(NOTIFICATION_FLAG, myNotify);
break;
// 默认通知 API11及之后可用
case R.id.btn2:
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// 通过Notification.Builder来创建通知,注意API Level
// API11之后才支持
Notification notify2 = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
// icon)
.setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status
// bar上显示的提示文字
.setContentTitle("Notification Title")// 设置在下拉status
// bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
.setContentText("This is the notification message")// TextView中显示的详细内容
.setContentIntent(pendingIntent2) // 关联PendingIntent
.setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
.getNotification(); // 需要注意build()是在API level
// 16及之后增加的,在API11中可以使用getNotificatin()来代替
notify2.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFICATION_FLAG, notify2);
break;
// 默认通知 API16及之后可用
case R.id.btn3:
PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// 通过Notification.Builder来创建通知,注意API Level
// API16之后才支持
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("TickerText:" + "您有新短消息,请注意查收!")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API
// level16及之后增加的,API11可以使用getNotificatin()来替代
notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
manager.notify(NOTIFICATION_FLAG, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
break;
// 自定义通知
case R.id.btn4:
// Notification myNotify = new Notification(R.drawable.message,
// "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());
myNotify = new Notification();
myNotify.icon = R.drawable.ic_launcher;
myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
myNotify.when = System.currentTimeMillis();
myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
myNotify.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯
myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失
rv = new RemoteViews(getPackageName(),
R.layout.my_notification);
rv.setTextViewText(R.id.text_content, "hello wrold!");
myNotify.contentView = rv;
// Intent intent = new Intent(Intent.ACTION_MAIN);
intent1 = new Intent(getApplicationContext(),
ContentActivity.class);
intent1.putExtra("content", "444");
intent1.putExtra("number", "444");
intent1.putExtra("date", "444");
contentIntent = PendingIntent.getActivity(this, 1,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
myNotify.contentIntent = contentIntent;
//第一个参数保证信息不一样的
manager.notify(NOTIFICATION_FLAG_4, myNotify);
break;
case R.id.btn5:
// 清除id为NOTIFICATION_FLAG的通知
// manager.cancel(NOTIFICATION_FLAG);
// manager.cancel(NOTIFICATION_FLAG_4);
// 清除所有的通知
manager.cancelAll();
break;
case R.id.btn6:
Pro_Notification();
break;
default:
/** * 有进度条的notification * @param view */
Pro_Notification();
break;
}
}
NotificationCompat.Builder notifyBuilder;
NotificationManager manager_n;
/** * 带滚动条的提示 */ public void Pro_Notification() { manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notifyBuilder = new NotificationCompat.Builder(this); notifyBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setTicker("TickerText:您有新下载消息,请注意查收!") .setOngoing(true) .setSmallIcon(R.drawable.ic_launcher); // Start a lengthy operation in a background thread new Thread( new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr += 5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state notifyBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. manager_n.notify(0, notifyBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep( 1000); } catch (InterruptedException e) { Log.d("NOTIFICATION", "sleep failure"); } } // When the loop is finished, updates the notification notifyBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0, 0, false); manager_n.notify(213, notifyBuilder.build()); } } // Starts the thread by calling the run() method in its Runnable ).start(); }
}
相关文章推荐
- Notification和NotificationManager的基本使用方法
- Android笔记】Notification和NotificationManager的基本使用方法(转)
- Android 通知栏 Notification 基本使用
- Notification使用总结(基本使用)
- android——Notification的基本使用
- 【Android笔记】Notification和NotificationManager的基本使用方法
- Android notification的基本使用
- 安卓中通知(Notification)的基本使用方法
- 通知栏(Notification)的基本使用
- Notification和NotificationManager的基本使用方法
- 【Android笔记】Notification和NotificationManager的基本使用方法
- Android——Notification和NotificationManager的基本使用方法
- notification常见的通知基本使用
- Notification的基本使用
- Notification和NotificationManager的基本使用方法
- Notification的基本使用方法
- 【Android笔记】Notification和NotificationManager的基本使用方法(转)
- notification 使用的基本方法
- 通知栏Notification基本使用
- 安卓中通知(Notification)的基本使用方法