notification

一般的notification

  1 package com.qianfeng.gp08_day22_notification;
  2
  3 import android.os.Bundle;
  4 import android.support.v4.app.NotificationCompat;
  5 import android.view.Gravity;
  6 import android.view.View;
  7 import android.view.View.OnClickListener;
  8 import android.widget.Toast;
  9 import android.app.Activity;
 10 import android.app.Notification;
 11 import android.app.NotificationManager;
 12 import android.app.PendingIntent;
 13 import android.content.Context;
 14 import android.content.Intent;
 15
 16
 17 /**
 18  * 使用的v4包中的NotificationCompat
 19  * @author qq
 20  *
 21  */
 22 public class MainActivity extends Activity{
 23
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28     }
 29
 30     public void btnClick(View v)
 31     {
 32         //创建通知 的构建器对象
 33         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 34
 35         //设置通知的相关属性
 36         builder.setContentTitle("通知");
 37
 38         builder.setContentText("下雨了");
 39
 40         //必须设置,否则程序会崩溃
 41         builder.setSmallIcon(R.drawable.ic_launcher);
 42
 43         //得到通知管理对象---属于系统服务
 44         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 45
 46         //得到通知对象
 47         Notification notification = builder.build();
 48
 49         //使用通知管理对象发送通知
 50         manager.notify((int)System.currentTimeMillis(), notification);//如果id相同,即使发送多个通知,也只显示一个
 51
 52     }
 53
 54     public void btnClick2(View v)
 55     {
 56         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 57
 58         builder.setContentTitle("你有短信");
 59         builder.setContentText("你有3条短信");
 60         builder.setSmallIcon(R.drawable.ic_launcher);
 61         builder.setNumber(3);//设置数量
 62         //收到通知可以 是声音,灯光,震动
 63         //DEFAULT_VIBRATE  需要权限
 64         builder.setDefaults(Notification.DEFAULT_VIBRATE);
 65
 66         Notification notification = builder.build();
 67
 68         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 69
 70         manager.notify(168, notification);
 71
 72     }
 73
 74     public void btnClick3(View v)
 75     {
 76         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 77
 78         builder.setContentTitle("下载图片");
 79         builder.setContentText("正在下载");
 80         builder.setSmallIcon(R.drawable.ic_launcher);
 81         builder.setOngoing(true);//设置不可以被删除
 82
 83
 84         Notification n = builder.build();
 85
 86         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 87
 88         manager.notify(188, n);
 89     }
 90
 91     public void btnClick4(View v)
 92     {
 93         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 94
 95         builder.setContentTitle("你有短信")
 96                .setContentText("你有6条短信")
 97                .setSmallIcon(R.drawable.ic_launcher)
 98                .setNumber(6)
 99                .setDefaults(Notification.DEFAULT_ALL);
100
101         Intent intent = new Intent(this,MainActivity.class);
102
103         //可以当通知被点击时自动执行 startActiivty()
104         PendingIntent pending = PendingIntent.getActivity(this, 6, intent, PendingIntent.FLAG_CANCEL_CURRENT);
105                                                                               //跳转后通知会消失
106         builder.setContentIntent(pending);
107
108         builder.setAutoCancel(true);//设置通知自动消失
109
110         Notification ntf = builder.build();
111
112         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
113
114         manager.notify(88, ntf);
115     }
116
117     public void btnClick5(View v)
118     {
119         Toast toast = new Toast(this);
120
121         View view = getLayoutInflater().inflate(R.layout.toast_layout, null);
122
123         toast.setView(view);//设置吐司使用的布局
124
125         toast.setGravity(Gravity.CENTER, 0,0);//设置吐司的显示位置
126
127         toast.setDuration(Toast.LENGTH_SHORT);
128
129         toast.show();
130     }
131
132
133
134
135
136 }

mainActivity.java

带进度条的异步下载notification

 1 package com.example.asynctasknotification;
 2
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.ImageView;
10
11 public class MainActivity extends Activity {
12
13     private ImageView imageView;
14     private Button btn;
15     private String path="http://images2015.cnblogs.com/blog/493196/201509/493196-20150901203057606-579869820.jpg";
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20
21         imageView=(ImageView) findViewById(R.id.iv);
22         btn=(Button) findViewById(R.id.btn);
23
24         btn.setOnClickListener(new OnClickListener() {
25
26             @Override
27             public void onClick(View v) {
28                 new MyAsyncTask(MainActivity.this,imageView).execute(path);
29
30             }
31         });
32
33
34
35
36     }
37
38     @Override
39     public boolean onCreateOptionsMenu(Menu menu) {
40         // Inflate the menu; this adds items to the action bar if it is present.
41         getMenuInflater().inflate(R.menu.main, menu);
42         return true;
43     }
44
45 }

mainActivity.java

  1 package com.example.asynctasknotification;
  2
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.net.HttpURLConnection;
  7 import java.net.MalformedURLException;
  8 import java.net.URL;
  9
 10 import android.app.Notification;
 11 import android.app.NotificationManager;
 12 import android.content.Context;
 13 import android.graphics.Bitmap;
 14 import android.graphics.BitmapFactory;
 15 import android.os.AsyncTask;
 16 import android.support.v4.app.NotificationCompat;
 17 import android.widget.ImageView;
 18
 19 public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
 20
 21     private NotificationCompat.Builder builder;
 22     private NotificationManager manager;
 23     private ImageView imageView;
 24     private Context context;
 25
 26     public MyAsyncTask(Context context, ImageView imageView) {
 27         this.imageView = imageView;
 28         this.context = context;
 29         manager = (NotificationManager) context
 30                 .getSystemService(Context.NOTIFICATION_SERVICE);
 31         builder = new NotificationCompat.Builder(context);
 32         builder.setContentTitle("下载图片");
 33         builder.setContentText("正在下载");
 34         builder.setSmallIcon(R.drawable.ic_launcher);
 35     }
 36
 37     @Override
 38     protected Bitmap doInBackground(String... params) {
 39
 40         Bitmap bitmap = null;
 41         URL url;
 42         try {
 43             url = new URL(params[0]);
 44             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 45             conn.setRequestMethod("GET");
 46             conn.setConnectTimeout(5000);
 47             conn.setDoInput(true);
 48
 49             InputStream in = null;
 50             if (conn.getResponseCode() == 200) {
 51                 in = conn.getInputStream();
 52                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 53                 int total = conn.getContentLength();
 54                 int len = 0;
 55                 int count = 0;
 56                 byte[] by = new byte[1024];
 57                 while ((len = in.read(by)) != -1) {
 58                     baos.write(by, 0, len);
 59                     count += len;
 60                     publishProgress((int) (100 * count / total));
 61                 }
 62                 byte[] b = baos.toByteArray();
 63                 bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
 64             }
 65         } catch (MalformedURLException e) {
 66             e.printStackTrace();
 67         } catch (IOException e) {
 68             e.printStackTrace();
 69         }
 70         return bitmap;
 71     }
 72
 73     @Override
 74     protected void onCancelled() {
 75         // TODO Auto-generated method stub
 76         super.onCancelled();
 77     }
 78
 79     @Override
 80     protected void onPreExecute() {
 81         super.onPreExecute();
 82
 83         builder.setOngoing(true);// 设置不可以被删除
 84         builder.setProgress(100, 0, false);
 85
 86         manager.notify(188, builder.build());
 87
 88     }
 89
 90     @Override
 91     protected void onPostExecute(Bitmap result) {
 92
 93         builder.setOngoing(false);// 设置可以被删除
 94         builder.setContentText("下载完毕");
 95         builder.setProgress(0, 0, false);
 96         manager.notify(188, builder.build());
 97
 98         imageView.setImageBitmap(result);
 99
100     }
101
102     @Override
103     protected void onProgressUpdate(Integer... values) {
104         builder.setProgress(100, values[0], false);
105         builder.setContentText("已下载"+values[0]+"%");
106         manager.notify(188, builder.build());
107     }
108
109 }

asynctask

时间: 2024-08-13 19:45:49

notification的相关文章

通知栏Notification在不同手机上显示的问题总结

可以参照http://blog.csdn.net/vipzjyno1/article/details/25248021,这里面关于通知的写的不错,也很全面,我的这篇主要是记录自己在适配上遇到的问题. 通知的统一的创建方式: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); 而通知的管理则是使用NotificationManager是用来管理通知的,使用如下:先初始化用到的系统服务,然后调

Apple Notification Center Service--ANCS【转】

Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 2014 | COMMENTS 名词解释与约定 名词解释 Apple Notification Center Service 简称 ANCS. ANCS 服务(iOS设备,如iPhone,iPad等)的publisher 称为 Notification Provider. 任意的ANCS服务的clien

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException

Android-自定义Notification

Android-自定义Notification 2014年4月26日 消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示.本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo. 先来看看效果图: 附上源码:http://download.csdn.net/detail/wwj_748/7259815 有兴趣的朋友可以加本人创建的群,里

通知(Notification)

1.通知的基本用法 //创建 NotificationManager 实例 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("This is title") //标题 .setContent

Notification的基本用法以及使用RemoteView实现自定义布局

Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) 显示正在进行的事物(后台运行的程序,如音乐播放进度.下载进度) Notification的基本操作: Notification的基本操作主要有创建.更新和取消三种.一个Notification的必要属性有三项,如果不设置的话在运行时会抛出异常: 小图标,通过setSmallIcon方法设置 标题,通

Android中使用Notification实现宽通知栏(Notification示例二)

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.通知栏和通知抽屉 (notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. Notification 的设计: 作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则. Notification的界面元素在通知抽屉中的

安卓学习之通知(Notification)

安卓中创建通知需要借助NotificationManager来对通知进行管理.可以调用Context的getsSystemService()方法来获得. getsSystemService()方法接收一个参数,这个参数是字符串,用于指定哪一个服务.Context.NOTIFICATION_SERVICE 就是指定通知服务. 这个方法返回一个Object对象,所欲需要进行强制转换. NotificationManager manager = (NotificationManager) getSys