Notification中更新进度条

有时我们在下载的时候可以在通知里看到进度条信息,我这里做了一个简单更新进度条的通知。

进度条布局如下notification.xml:


android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="fill_parent" />

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/image"

android:textColor="#FFF" />

android:id="@+id/progress_horizontal"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/text"

android:layout_toRightOf="@+id/image"

android:max="100"

android:progress="50" />

主要的Activity代码如下:

package com.lml.notification;

import java.util.Timer;

import java.util.TimerTask;

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.os.Message;

import android.view.View;

import android.widget.Button;

import android.widget.RemoteViews;

public class MainActivity extends Activity {

RemoteViews remoteView;

int num=0;

Timer timer;

Notification noti ;

NotificationManager mnotiManager;

MyTimerTask task;

boolean isFirst=true;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button sendNotiBt = (Button) findViewById(R.id.sendNotiBt);

sendNotiBt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

showCustomizeNotification();

}

});

}

private void showCustomizeNotification() {

CharSequence title = "New Notification!";

Notification.Builder builder = new Notification.Builder(MainActivity.this);

builder.setTicker(title);

builder.setSmallIcon(R.drawable.ic_launcher);

builder.setWhen(System.currentTimeMillis());

builder.setAutoCancel(true);

builder.setContentIntent(PendingIntent.getActivity(MainActivity.this, 0, new Intent(Intent.ACTION_DELETE), 0));

noti = builder.build();

remoteView = new RemoteViews(this.getPackageName(), R.layout.notification);

remoteView.setProgressBar(R.id.progress_horizontal, 100, 0, false);

remoteView.setImageViewResource(R.id.image, R.drawable.ic_launcher);

remoteView.setTextViewText(R.id.text, "我的新通知");

noti.contentView = remoteView;

if(isFirst){

timer = new Timer(true);

task =new MyTimerTask();

timer.schedule(task,0, 1000);

isFirst=false;

}else {

timer.cancel();

task.cancel();

timer = new Timer(true);

task =new MyTimerTask();

timer.schedule(task,0, 1000);

num=0;

}

mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mnotiManager.notify(R.string.app_name, noti);

}

Handler handler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

num+=10;

remoteView.setProgressBar(R.id.progress_horizontal, 100, num, false);

noti.contentView = remoteView;

mnotiManager.notify(R.string.app_name, noti);

if(num>=100){

timer.cancel();

task.cancel();

}

break;

}

super.handleMessage(msg);

}

};

private class MyTimerTask extends TimerTask{

@Override

public void run() {

Message message = new Message();

message.what = 1;

handler.sendMessage(message);

}

}

}

这里我用了定时器来更新进度,可以根据实际情况作相应修改。

你可能感兴趣的:(Notification中更新进度条)