当前位置: 首页 > 编程笔记 >

Android实现为Notification加上一个进度条的方法

邹斌
2023-03-14
本文向大家介绍Android实现为Notification加上一个进度条的方法,包括了Android实现为Notification加上一个进度条的方法的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了Android实现为Notification加上一个进度条的方法。分享给大家供大家参考,具体如下:

package com.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class nofificationActivity extends Activity implements OnClickListener {
  private static final int NOTIFICATION_ID = 0x12;
  private Notification notification = null;
  private NotificationManager manager = null;
  public Handler handler;
  private int _progress = 0;
  private Thread thread = null;
  private boolean isStop = false;
  // 当界面处理停止的状态 时,设置让进度条取消
  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    isStop = false;
    manager.cancel(NOTIFICATION_ID);
    super.onPause();
  }
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(this);
    notification = new Notification(R.drawable.icon, "带进条的提醒", System
        .currentTimeMillis());
    notification.icon = R.drawable.icon;
    // 通过RemoteViews 设置notification中View 的属性
    notification.contentView = new RemoteViews(getApplication()
        .getPackageName(), R.layout.custom_dialog);
    notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
    notification.contentView.setTextViewText(R.id.tv, "进度" + _progress
        + "%");
    // 通过PendingIntetn
    // 设置要跳往的Activity,这里也可以设置发送一个服务或者广播,
    // 不过在这里的操作都必须是用户点击notification之后才触发的
    notification.contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, remoteView.class), 0);
    // 获得一个NotificationManger 对象,此对象可以对notification做统一管理,只需要知道ID
    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    isStop = true;
    manager.notify(NOTIFICATION_ID, notification);
    thread = new Thread(new Runnable() {
      @Override
      public void run() {
        Thread.currentThread();
        // TODO Auto-generated method stub
        while (isStop) {
          _progress += 10;
          Message msg = handler.obtainMessage();
          msg.arg1 = _progress;
          msg.sendToTarget();
          try {
            Thread.sleep(500);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    });
    thread.start();
    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        notification.contentView.setProgressBar(R.id.pb, 100, msg.arg1,
            false);
        notification.contentView.setTextViewText(R.id.tv, "进度"
            + msg.arg1 + "%");
        manager.notify(NOTIFICATION_ID, notification);
        if (msg.arg1 == 100) {
          _progress = 0;
          manager.cancel(NOTIFICATION_ID);
          isStop = false;
          Toast.makeText(nofificationActivity.this, "下载完毕", 1000)
              .show();
        }
        super.handleMessage(msg);
      }
    };
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android视图View技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

 类似资料:
  • 本文向大家介绍Android实现个性化的进度条,包括了Android实现个性化的进度条的使用技巧和注意事项,需要的朋友参考一下 1.案例效果图 2.准备素材 progress1.png(78*78) progress2.png(78*78) 3.原理 采用一张图片作为ProgressBar的背景图片(一般采用颜色比较浅的)。另一张是进度条的图片(一般采用颜色比较深的图片)。进度在滚动时:进度图片逐

  • 本文向大家介绍js实现进度条的方法,包括了js实现进度条的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了js实现进度条的方法。分享给大家供大家参考。具体实现方法如下: 1.setTimeout和clearTimeout 效果图: 2.setInterval和clearInterval 效果图: 3.setTimeout和setInterval区别 setTimeout() 只执行 c

  • 实现下图效果 要求 实际分数和刻度对应 350-550[2色渐变];550-600[3色渐变];600-650[4色渐变];650-700[5色渐变];700以上[6色渐变] 渐变起始位置为刻度表起始位置,终点位置为分数终点 其他简单的要求,比如字体颜色渐变啥的,就不列出来了 分析与思考 上面那些要求,是我拿到图后分解后的几个要求,由于分数是动态的,因此这里肯定不能直接丢个png上去,考虑了svg

  • 本文向大家介绍Android实现环形进度条,包括了Android实现环形进度条的使用技巧和注意事项,需要的朋友参考一下 一个通俗易懂的环形进度条,可以定制颜色角度,监听进度。 定义一个attrs.xml 自定义CircleProgressView 代码就这么些,接下来我们测算一下 源码:下载地址 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Ajax 实现加载进度条,包括了Ajax 实现加载进度条的使用技巧和注意事项,需要的朋友参考一下 ajax beforeSend: 先说说这个 beforeSend, 在请求发送前执行 比如 可以判断用户有没有登录 ,如果没有登录就停止请求 并提示。 $.ajax有一个参数是complete:function(){} 是在 请求完成之后执行的 ,配合beforeSend可以用来展示进

  • 本文向大家介绍Android实现环形进度条的实例,包括了Android实现环形进度条的实例的使用技巧和注意事项,需要的朋友参考一下 Android实现环形进度条的效果图如下: 自定义控件:AttendanceProgressBar 代码如下: 因为是自定义控件,所以在attr.xml文件定义了一些控件属性,以便在xml文件中设置这些属性 代码如下: 最后,在xml文件中,可以这样使用 这只是初步处