android-带进度条的系统通知栏消息
2024-08-27 09:52:12
效果图:
主界面只有一个按钮就不上文件了
通知栏显示所用到的布局文件content_view.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00000000"
- android:orientation="vertical"
- android:padding="5dp">
- <ImageView
- android:id="@+id/content_view_image"
- android:layout_width="25dp"
- android:layout_height="25dp"
- android:src="@drawable/logo"
- />
- <TextView
- android:id="@+id/content_view_text1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0%"
- android:textColor="#000000"
- android:layout_toRightOf="@id/content_view_image"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="5dp"
- android:layout_marginLeft="15dp"
- />
- <ProgressBar
- android:id="@+id/content_view_progress"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:max="100"
- android:layout_below="@id/content_view_image"
- android:layout_marginTop="4dp"
- />
- </RelativeLayout>
主运行类:
- package yyy.testandroid4;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.app.AlertDialog.Builder;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager.NameNotFoundException;
- 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 TestAndroid4Activity extends Activity {
- private Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- super.handleMessage(msg);
- switch (msg.what) {
- case 0:
- notif.contentView.setTextViewText(R.id.content_view_text1, len+"%");
- notif.contentView.setProgressBar(R.id.content_view_progress, 100, len, false);
- manager.notify(0, notif);
- break;
- case 1:
- Toast.makeText(TestAndroid4Activity.this, "下载完成", 0).show();
- break;
- default:
- break;
- }
- }
- };
- private Button update,cancel;
- private int localVersion,serverVersion;
- private int len;
- private NotificationManager manager;
- private Notification notif;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- update = (Button) findViewById(R.id.update);
- update.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- //点击通知栏后打开的activity
- Intent intent = new Intent(TestAndroid4Activity.this,OtherActivity.class);
- PendingIntent pIntent = PendingIntent.getActivity(TestAndroid4Activity.this, 0, intent, 0);
- manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- notif = new Notification();
- notif.icon = R.drawable.logo;
- notif.tickerText = "新通知";
- //通知栏显示所用到的布局文件
- notif.contentView = new RemoteViews(getPackageName(), R.layout.content_view);
- notif.contentIntent = pIntent;
- manager.notify(0, notif);
- new DownLoadThread().start();
- }
- });
- }
- }
- private class DownLoadThread extends Thread{
- private Timer timer = new Timer();
- @Override
- public void run() {
- // TODO Auto-generated method stub
- super.run();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Message msg = new Message();
- msg.what = 0;
- msg.obj = len;
- handler.sendMessage(msg);
- if(len == 100){
- timer.cancel();
- handler.sendEmptyMessage(1);
- }
- }
- }, 0, 1000);
- len = 0;
- try {
- while(len < 100){
- len++;
- Thread.sleep(1000);
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
最新文章
- 初步了解nodejs
- java高新技术-代理
- C#路径/文件/目录/I/O常见操作汇总
- javascript之DOM篇二(操作)
- ARC Rules
- 9. shell环境
- Servlet页面跳转实现方法的区别
- MyEclipse中消除frame引起的“the file XXX can not be found.Please check the location and try again.”的错误
- 开源yYmVc项目 v 0.2 版本号介绍
- Lumen 配置、重写、404错误等
- 【JS】cookies 的使用
- MYSQL的学习
- maven中jar下载失败
- NIO相关
- xl2tpd[26104]: Maximum retries exceeded for tunnel 33925. Closing
- Python3.6连接mysql(一)
- docker4种网络最佳实战 --摘自https://www.cnblogs.com/iiiiher/p/8047114.html
- byteify
- 关于网站中Logo部分的写法
- RabbitMQ:基本命令
热门文章
- 您在基于 Windows 7 的或基于 Windows Server 2008 R2 的计算机上读取器中插入智能卡时出现错误消息:";设备驱动程序软件未能成功安装";
- ArcGIS学习推荐基础教程摘录
- linux中ctrl+z、ctrl+d和ctrl+c的区别
- Linux学习之十一、环境变量的功能
- SQL Server中的DATEPART函数的使用
- 使用SuperWebSocket 构建实时 Web 应用
- send js object to webapi or mvc
- 配置Nutch模拟浏览器以绕过反爬虫限制
- sqlserver查询某一字段重复超5次的所有记录
- 关于新装ubuntu系统update失败和build-essential失败的解决办法