在android运用中,经常可以看到在下载东西的时候可以有个进度条的玩意儿显示在状态栏中。
参考网上的资料后,自己动手做了一个,结构不一定科学,但是功能确实是实现了。
首先是两个xml文件:
第一个是主页面,上面只有一个按钮而已。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<Button android:text="Notification" android:id="@+id/button1"
android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
第二个xml页面,包括一个imageview、progressbar、textview。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<ImageView android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="@drawable/icon"></ImageView>
<ProgressBar style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content" android:layout_width="180dip"
android:id="@+id/progressBar1" android:layout_marginTop="13dip" ></ProgressBar>
<TextView android:text="TextView" android:layout_height="wrap_content"
android:id="@+id/textView1" android:layout_width="wrap_content"
android:textSize="12sp" android:layout_marginTop="13dip"></TextView>
</LinearLayout>
其次是两个java文件
ForAndroidActivity.java
package fover.dialog;
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;
public class ForAndroidActivity extends Activity implements OnClickListener {
private NotificationManager manager;
private RemoteViews view;
private Notification notification;
private PendingIntent pIntent;
private final static int RUN = 1;
private final static int STOP = 0;
private int progressNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notify);
// 取得notification的服务,这样才能控制它撒
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// new一个notification来设置它的发生时间,一般用当前时间。
notification = new Notification(R.drawable.icon, "这里显示您第一眼看到这个消息的信息",
System.currentTimeMillis());
// 将另一个用来显示在通知栏上的layout加载进来
view = new RemoteViews(getPackageName(), R.layout.main);
// 设置点击通知栏后需要启动的activity
Intent intent = new Intent(ForAndroidActivity.this, Text.class);
// 设置PendingIntent,需要注意的是这里使用的是getActivity这个方法
pIntent = PendingIntent.getActivity(ForAndroidActivity.this, 0, intent,
0);
// 发现按钮并且设置监听事件
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// new一个线程用于发送消息
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
progressNumber = (i + 1) * 5;
try {
if (i < 19) {
Message msg = new Message();
msg.what = RUN;
myHandler.sendMessage(msg);
Thread.sleep(1000);
} else {
Message msg = new Message();
msg.what = STOP;
myHandler.sendMessage(msg);
Thread.currentThread().interrupt();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
break;
default:
break;
}
}
// new一个handler来接收处理消息,并且更新界面。
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case RUN:
// 注意此法,可以用来设置加载进来的那个view的页面元素
view.setProgressBar(R.id.progressBar1, 100, progressNumber,
false);
view.setTextViewText(R.id.textView1, "下载完成" + progressNumber
+ "%");
notification.contentView = view;
notification.contentIntent = pIntent;
manager.notify(0, notification);
break;
case STOP:
notification.icon = R.drawable.icon;
notification.setLatestEventInfo(ForAndroidActivity.this,
"下载完成", "您下载的ABS130已经下载完了", pIntent);
// 进度条完成之后发出提示声音
notification.defaults=Notification.DEFAULT_SOUND;
manager.notify(0, notification);
default:
break;
}
super.handleMessage(msg);
}
};
}
另一个文件text.java
package fover.dialog;
import android.app.Activity;
import android.os.Bundle;
public class Text extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//国际惯例,加载layout文件
setContentView(R.layout.main);
}
}
运行效果: