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

进度条布局如下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);

}

}

}

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

更多相关文章

  1. 全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本
  2. android通知栏进度条
  3. android双服务,消息通知(manifest)
  4. Android Wear Preview- 创建通知(Creating Notifications for And
  5. 【Android】状态栏通知Notification、NotificationManager详解
  6. Android三种实现定时器的方法
  7. Android显示横幅样式通知

随机推荐

  1. 技术博客
  2. Android(安卓)Studio中如何查看获取MD5和
  3. 基于unity3d游戏的android版本逆向初探
  4. Android初级教程八——TextView编程实现
  5. Litepal的简单使用
  6. Android(安卓)锁屏状态下拉起某个页面
  7. Android(安卓)多线程之HandlerThread 完
  8. Andriod软键盘出现把原来的布局给顶上去
  9. WebView Android(安卓)调用js且需要获取
  10. Android判断后台Service服务是否运行,获取