通知
setDefaults(): 这只通知LED等、音乐、震动等
setAutoCancle():点击通知后,是否自动消失
setContentTitle():通知内容标题
setContentText():通知内容
setSmailIcom():通知小图标
SetLargeIcon():通知大图标
setTick():通知状态栏提示信息
setContentIntent():双击后运行的Intent
直接上例子,第一个简单点,就一个提示信息,点击后调用一个页面
第二个显示一个下载的提示信息,带进度
例子1
activity_main.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭通知" /> </LinearLayout>
MainActivity.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class MainActivity extends Activity { NotificationManager nm; Button btn1; Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取系统的NotificationManager服务 nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(MainActivity.this,LoginActivity.class); //对intent进行包装 PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //创建通知 Notification nf=new Notification.Builder(MainActivity.this) //是否自动关闭 .setAutoCancel(true) //通知的提示信息 .setTicker("有新消息") //小图标 .setSmallIcon(R.drawable.ic_launcher) //通知标题 .setContentTitle("一条新的通知") //通知内容 .setContentText("有新的通知,点击查看") //设置系统默认声音,默认LED灯 //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS) //设置自定义声音 //.setSound(sound) //什么时候启动 .setWhen(System.currentTimeMillis()) //设置该通知要启动的intent .setContentIntent(pi).build(); //发送 nm.notify(1234, nf); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //取消 nm.cancel(1234); } }); } }
例子2
一个下载升级包的例子,点击开始下载,通知栏会增加一条现实进度的消息,下载完成了,点击可安装apk
activity_main.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android1:id="@+id/button1" android1:layout_width="wrap_content" android1:layout_height="wrap_content" android1:text="开始下载" /> </LinearLayout>
自定义xml
view1.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="XXX.apk" android:textAppearance="?android:attr/textAppearanceSmall" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_toLeftOf="@+id/textView3" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/progressBar1" android:text="100K/1024K" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/progressBar1" android:text="90%" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/progressBar1" android:layout_below="@+id/textView3" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
MainActivity.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class MainActivity extends Activity{ NotificationManager nm; RemoteViews rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(MainActivity.this,DownService.class); intent.putExtra("appName", "shenghuoriliwannianli_39"); intent.putExtra("appURL", "http://gdown.baidu.com/data/wisegame/2dcb384356f57ea2/shenghuoriliwannianli_39.apk"); startService(intent); } }); } }
DownService.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class DownService extends Service{ private String appName; private String appURL; private NotificationManager ni; private Notification nf; private RemoteViews rv; private File updateFile; @Override public IBinder onBind(Intent intent) { // TODO 自动生成的方法存根 return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { appName=intent.getExtras().getString("appName"); appURL=intent.getExtras().getString("appURL"); //升级目录 File updateDir = new File(Environment.getExternalStorageDirectory()+ "/test/"); //升级文件 updateFile = new File(updateDir + "/" + appName + ".apk"); //判断文件夹是否存在,不存在则创建 if(!updateDir.exists()){ updateDir.mkdir(); } //判断升级文件是否存在,不存在则创建 if(!updateFile.exists()){ try { updateFile.createNewFile(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } //创建NotificationManager ni=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); //创建notification nf=new Notification(R.drawable.ic_launcher,"下载",System.currentTimeMillis()); //将此通知放到通知栏的"Ongoing"即"正在运行"组中 nf.flags |= Notification.FLAG_ONGOING_EVENT; //定义notification显示 rv=new RemoteViews(getPackageName(),R.layout.view1); rv.setProgressBar(R.id.progressBar1, 100, 0, true); rv.setTextViewText(R.id.textView1, appName); rv.setTextViewText(R.id.textView3, "0%"); rv.setTextViewText(R.id.textView2, "0K/0K"); nf.contentView=rv; ni.notify(R.layout.view1, nf); //开始下载 new DownLoadThread().start(); return super.onStartCommand(intent, flags, startId); } //定义一个handler,用来刷新界面 Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { //刷新数据 if(msg.what==0x01){ int downloadSzie=msg.getData().getInt("downloadSzie"); int totalSize=msg.getData().getInt("totalSize"); //没下载完 int pos=downloadSzie*100/totalSize; rv.setTextViewText(R.id.textView3, pos+"%"); rv.setTextViewText(R.id.textView2, (downloadSzie/1024)+"K/"+(totalSize/1024)+"K"); //下载完了 if(downloadSzie==totalSize){ rv.setTextViewText(R.id.textView4,"下载完成,点击安装"); Intent in=new Intent(Intent.ACTION_VIEW); String path=updateFile.getPath(); Uri uri=Uri.fromFile(updateFile); in.setDataAndType(uri, "application/vnd.android.package-archive"); PendingIntent pendingIntent = PendingIntent.getActivity(DownService.this, 0, in, 0); //修改标题类型,点击一次后关闭 nf.flags = Notification.FLAG_AUTO_CANCEL; nf.contentIntent=pendingIntent; } nf.contentView=rv; ni.notify(R.layout.view1, nf); } } }; //定义一个下载线程 class DownLoadThread extends Thread { private int totalSize=0; private int downloadSzie=0; @Override public void run(){ try { URL url; HttpURLConnection http; url = new URL(appURL); http=(HttpURLConnection)url.openConnection(); //记录上次百分比,主要是为了减少界面刷新 int pos=0; //timeout 10秒 http.setConnectTimeout(10*1000); http.setReadTimeout(10*1000); //获取文件大小 totalSize=http.getContentLength(); //成功 if(http.getResponseCode()==200){ InputStream in=http.getInputStream(); FileOutputStream out=new FileOutputStream(updateFile,false); byte[] buffer =new byte[1024]; int readSize=0; while ((readSize=in.read(buffer))!=-1){ //保存到文件 out.write(buffer,0,readSize); //总下载数 downloadSzie =downloadSzie +readSize; //刷新界面 if(pos!=(downloadSzie*100/totalSize)){ Message msg=new Message(); msg.what=0x01; Bundle b=new Bundle(); b.putInt("downloadSzie", downloadSzie); b.putInt("totalSize", totalSize); msg.setData(b); handler.sendMessage(msg); pos=downloadSzie*100/totalSize; } } if(http!=null){ http.disconnect(); } in.close(); out.close(); } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }