android自定义Notification通知栏实例

时间:2023-03-09 04:39:19
android自定义Notification通知栏实例

项目有个需求,需要在发送Notification的时候动态给定url的图片。大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性。

首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后,设置到自定义布局文件中的ImageView上。

自定义通知栏Notification布局如下:

[java] view plaincopyandroid自定义Notification通知栏实例android自定义Notification通知栏实例
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <ImageView
  6. android:id="@+id/image"
  7. android:layout_width="45dip"
  8. android:layout_height="45dip"
  9. android:layout_alignParentLeft="true"
  10. android:layout_marginBottom="8.0dip"
  11. android:layout_marginLeft="8.0dip"
  12. android:layout_marginRight="10dp"
  13. android:layout_marginTop="8.0dip" />
  14. <TextView
  15. android:id="@+id/title"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginTop="8.0dip"
  19. android:layout_toRightOf="@id/image"
  20. android:textSize="16.0dip" />
  21. <TextView
  22. android:id="@+id/text"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_below="@id/title"
  26. android:layout_marginTop="3.0dip"
  27. android:layout_toRightOf="@id/image"
  28. android:textSize="16.0dip" />
  29. <TextView
  30. android:id="@+id/time"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignParentRight="true"
  34. android:layout_centerVertical="true"
  35. android:layout_marginRight="8.0dip"
  36. android:textSize="16.0dip" />
  37. </RelativeLayout>

android代码:

[java] view plaincopyandroid自定义Notification通知栏实例android自定义Notification通知栏实例
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import android.app.Activity;
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.RemoteViews;
  19. public class MainActivity extends Activity {
  20. private String url = "http://www.takungpao.com/world/content/image/attachement/jpg/site2/20120605/d4bed9b92d221137df0511.jpg";
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. set(url);
  29. }
  30. });
  31. }
  32. public void set(String urlStr) {
  33. new AsyncTask<String, Void, Bitmap>() {
  34. @Override
  35. protected Bitmap doInBackground(String... params) {
  36. try {
  37. URL url = new URL(params[0]);
  38. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  39. conn.setConnectTimeout(6000);//设置超时
  40. conn.setDoInput(true);
  41. conn.setUseCaches(false);//不缓存
  42. conn.connect();
  43. int code = conn.getResponseCode();
  44. Bitmap bitmap = null;
  45. if(code==200) {
  46. InputStream is = conn.getInputStream();//获得图片的数据流
  47. bitmap = BitmapFactory.decodeStream(is);
  48. }
  49. return bitmap;
  50. } catch (MalformedURLException e) {
  51. e.printStackTrace();
  52. return null;
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. return null;
  56. }
  57. }
  58. @Override
  59. protected void onPostExecute(Bitmap result) {
  60. super.onPostExecute(result);
  61. if (result != null) {
  62. showNotification(result);
  63. }
  64. }
  65. }.execute(urlStr);
  66. }
  67. private void showNotification(Bitmap bitmap){
  68. NotificationManager manager = (NotificationManager) MainActivity.this
  69. .getSystemService(Context.NOTIFICATION_SERVICE);
  70. Notification noti = new Notification();
  71. noti.flags = Notification.FLAG_AUTO_CANCEL;
  72. noti.icon = R.drawable.ic_launcher;
  73. // 1、创建一个自定义的消息布局 notification.xml
  74. // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
  75. RemoteViews rv = new RemoteViews(this.getPackageName(),
  76. R.layout.cus_noti);
  77. rv.setImageViewResource(R.id.image,
  78. R.drawable.ic_launcher);
  79. rv.setImageViewBitmap(R.id.image, bitmap);
  80. rv.setTextViewText(R.id.text,
  81. "Hello,this message is in a custom expanded view");
  82. noti.contentView = rv;
  83. // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
  84. // 这儿点击后简答启动Settings模块
  85. PendingIntent contentIntent = PendingIntent.getActivity
  86. (MainActivity.this, 0,new
  87. Intent("android.settings.SETTINGS"), 0);
  88. noti.contentIntent = contentIntent;
  89. manager.notify(1, noti);
  90. }
  91. }