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
        }
    };
}

猜你喜欢

转载自blog.csdn.net/qq_24800377/article/details/79042871