1. 程式人生 > >android自定義notification實現進度條功能

android自定義notification實現進度條功能

要建立一個自定義的Notification,可以使用RemoteViews。要定義自己的擴充套件訊息,首先要初始化一個RemoteViews物件,然後將它傳遞給Notification contentView欄位,再把PendingIntent傳遞給contentIntent欄位。以下用一個小例子來說明:

自定義notification的檢視view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical">
<ImageView android:id="@+id/iv" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" />
<ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="3" /> <TextView android:id="@+id/tv" android:layout_width
="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="0%" />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

activity_main.xml佈局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/bt_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="傳送通知" />

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

MainActivity.java類

package com.example.demonotificationuserdefine;

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.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

    private Button bt_send;
    private NotificationManager nm;
    private Notification notification;
    private Handler handler = new Handler();

    private int progress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        bt_send = (Button) findViewById(R.id.bt_send);
        bt_send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                nm.notify(1, notification);
                handler.post(runn);
            }
        });
    }

    @SuppressWarnings("deprecation")
    public void initData() {
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notification = new Notification(R.drawable.ic_launcher, "新訊息",
                System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(getPackageName(),
                R.layout.view);
        contentView.setImageViewResource(R.id.iv, R.drawable.ic_launcher);
        contentView.setProgressBar(R.id.pb, 100, progress, false);
        contentView.setTextViewText(R.id.tv, "進度條");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(MainActivity.this,
                MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
    }

    private Runnable runn = new Runnable() {

        @Override
        public void run() {

            progress++;
            notification.contentView.setProgressBar(R.id.pb, 100, progress,
                    false);
            nm.notify(1, notification);// 更新notification,即更新進度條
            if (progress < 100)
                handler.postDelayed(runn, 200); // 200毫秒progress加1
        }
    };
}