android自定義Notification通知欄例項
阿新 • • 發佈:2019-01-28
專案有個需求,需要在傳送Notification的時候動態給定url的圖片。大概思路如下:自己定義一個Notification的佈局檔案,這樣能夠很方便設定View的屬性。
首先載入網路圖片,使用BitmapFactory.decodeStream解析出Bitmap,然後,設定到自定義佈局檔案中的ImageView上。
自定義通知欄Notification佈局如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="45dip"
- android:layout_height="45dip"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="8.0dip"
- android:layout_marginLeft="8.0dip"
- android:layout_marginRight="10dp"
- android:layout_marginTop="8.0dip" />
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/title"
- android:layout_marginTop="3.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="8.0dip"
- android:textSize="16.0dip" />
- </RelativeLayout>
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- 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.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.RemoteViews;
- publicclass MainActivity extends Activity {
- private String url = "http://www.takungpao.com/world/content/image/attachement/jpg/site2/20120605/d4bed9b92d221137df0511.jpg";
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- set(url);
- }
- });
- }
- publicvoid set(String urlStr) {
- new AsyncTask<String, Void, Bitmap>() {
- @Override
- protected Bitmap doInBackground(String... params) {
- try {
- URL url = new URL(params[0]);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(6000);//設定超時
- conn.setDoInput(true);
- conn.setUseCaches(false);//不快取
- conn.connect();
- int code = conn.getResponseCode();
- Bitmap bitmap = null;
- if(code==200) {
- InputStream is = conn.getInputStream();//獲得圖片的資料流
- bitmap = BitmapFactory.decodeStream(is);
- }
- return bitmap;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- returnnull;
- } catch (IOException e) {
- e.printStackTrace();
- returnnull;
- }
- }
- @Override
- protectedvoid onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- if (result != null) {
- showNotification(result);
- }
- }
- }.execute(urlStr);
- }
- privatevoid showNotification(Bitmap bitmap){
- NotificationManager manager = (NotificationManager) MainActivity.this
- .getSystemService(Context.NOTIFICATION_SERVICE);
- Notification noti = new Notification();
- noti.flags = Notification.FLAG_AUTO_CANCEL;
- noti.icon = R.drawable.ic_launcher;
- // 1、建立一個自定義的訊息佈局 notification.xml
- // 2、在程式程式碼中使用RemoteViews的方法來定義image和text。然後把RemoteViews物件傳到contentView欄位
- RemoteViews rv = new RemoteViews(this.getPackageName(),
- R.layout.cus_noti);
- rv.setImageViewResource(R.id.image,
- R.drawable.ic_launcher);
- rv.setImageViewBitmap(R.id.image, bitmap);
- rv.setTextViewText(R.id.text,
- "Hello,this message is in a custom expanded view");
- noti.contentView = rv;
- // 3、為Notification的contentIntent欄位定義一個Intent(注意,使用自定義View不需要setLatestEventInfo()方法)
- // 這兒點選後簡答啟動Settings模組
- PendingIntent contentIntent = PendingIntent.getActivity
- (MainActivity.this, 0,new
- Intent("android.settings.SETTINGS"), 0);
- noti.contentIntent = contentIntent;
- manager.notify(1, noti);
- }
- }