标签:
一般的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 }
带进度条的异步下载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 }
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 }
标签:
原文地址:http://www.cnblogs.com/bimingcong/p/4822353.html