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

进度条布局如下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(安卓)Notification的使用
  2. Android(安卓)Camera使用小结
  3. Android(安卓)Camera使用小结
  4. android studio ndk 编译自定义MK文件
  5. Android(安卓)Camera使用小结
  6. Android(安卓)Studio-Gradle项目中添加JNI生成文件(.so文件)
  7. Android资源汇总---持续整理更新中!!!
  8. Android(安卓)对话框【Dialog】去除白色边框代码
  9. Android(安卓)Camera使用小结

随机推荐

  1. android 检测耳机是否插入
  2. android全屏,去掉title栏的办法
  3. 关于Android(安卓)ListView组件中android
  4. Android(安卓)Studio-Gradle项目中添加JN
  5. android Sqlite多线程访问异常解决方案
  6. Android屏幕相关设置
  7. Android(安卓)Permission denied(
  8. Android(安卓)Camera使用小结
  9. VMware虚拟机Ubuntu1804编译Android(安卓
  10. selector属性介绍