最近在学习如何发通知,Notification通知,是Android信息提示的手段之一,下面说说如何发送标准的通知,自定义通知,还有清除所有通知
如图:
点击按钮“发送通知(标准)”,发送标准定义的通知
点击按钮“发送通知(自定义)”,发送自定义的通知
点击按钮“清除所有通知”,清除的通知
废话少说,直接看代码:
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: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=".MainActivity" android:orientation="vertical"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发出通知(标准)" android:id="@+id/btn1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发出通知(自定义)" android:id="@+id/btn2" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="清除所有通知" android:id="@+id/btn3" /> </LinearLayout>
my.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" > <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="0" style="?android:attr/progressBarStyleHorizontal" android:id="@+id/my_progress" /> </LinearLayout>
MainActivity.java代码:
package com.example.notificationdemo; import java.util.ArrayList; import java.util.Random; import android.app.Activity; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; public class MainActivity extends Activity { /* * * Notification通知,是Android信息提示的手段之一 * 标准的通知 * 自定义通知 */ //ids用来保存通知的id ArrayList<Integer> ids=new ArrayList(); int index=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.btn1); Button btn2=(Button)findViewById(R.id.btn2); Button btn3=(Button)findViewById(R.id.btn3); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //1.创建通知对象 //参数1,是通知图标,参数2,是通知标题,参数3,通知发出的时间 Notification notification=new Notification(R.drawable.ic_launcher, "这是一个测试通知",System.currentTimeMillis()); //2.设置通知的内容主体 //PendingIntent是一个用于保存状态的意图 //由于通知可以脱离程序(发出之后将程序结束),所以我们需要一个对象来维持通知和程序之间的关系 PendingIntent pending=PendingIntent.getActivity(MainActivity.this, 1, new Intent(MainActivity.this,MainActivity.class), 0); notification.setLatestEventInfo(MainActivity.this, "通知标题", "这条信息是标准通知",pending); //设置通知不可被清除|被点击之后自动清除 notification.flags=Notification.FLAG_NO_CLEAR|Notification.FLAG_AUTO_CANCEL; //3.通过通知管理器将该通知发出 //NotificationManager是系统服务,可用来发送全局通知Notification NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Random rd= new Random(); //这个id表示通知的标识 int id=rd.nextInt(); manager.notify(id, notification); ids.add(id); } }); //自定义通知 btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final Notification notification=new Notification(R.drawable.ic_launcher, "这是自定义的通知", System.currentTimeMillis()); RemoteViews rv=new RemoteViews(MainActivity.this.getPackageName(), R.layout.my); notification.contentView=rv; notification.contentIntent=PendingIntent.getActivity(MainActivity.this, 1, new Intent(MainActivity.this,MainActivity.class), 0); notification.flags=Notification.FLAG_NO_CLEAR|Notification.FLAG_AUTO_CANCEL; final NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, notification); //启动一个任务,模拟进度 new Thread(){ public void run(){ while(index<100){ notification.contentView.setProgressBar(R.id.my_progress, 100,index, false); index++; try{ sleep(new Random().nextInt(201)+300); }catch(Exception e){ e.printStackTrace(); } //更新通知信息,id相同情况下覆盖老信息 manager.notify(1, notification); } } }.start(); } }); //清除所有保存的通知 btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); for(int i=0;i<ids.size();i++){ manager.cancel(ids.get(i)); } } }); } }
注释写的挺详细的了,如有疑问,欢迎留言