我们在做AndroID开发的时候经常会遇到后台线程执行的比如说下载文件的时候,这个时候我们希望让客户能看到后台有 *** 作进行,这时候我们就可以使用进度条,那么既然在后台运行,为的就是尽量不占用当前 *** 作空间,用户可能还要进行其他 *** 作,最好的方法就是在通知栏有个通知消息并且有个进度条。本文给一个例子工读者参考.
效果图如下:
主界面只有一个按钮就不上文件了
通知栏显示所用到的布局文件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" 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,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); } } },1000); len = 0; try { while(len < 100){ len++; Thread.sleep(1000); } } catch (InterruptedException e) { // Todo auto-generated catch block e.printstacktrace(); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android项目实现带进度条的系统通知栏消息全部内容,希望文章能够帮你解决android项目实现带进度条的系统通知栏消息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)