谁知道怎样在安卓手机通知栏里显示下载进度条啊?

谁知道怎样在安卓手机通知栏里显示下载进度条啊?,第1张

通知栏显示所用到的布局文件content_view.xml

主运行类:

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()

}

}

}

}

如果是第一次导入的项目,她会去网络仓库下载你build.gride里导入的包比如http请求的尺毕,或者第三方组件的包。会有各种类似下载的进度条。之后打陵迟芹开都是检查版本什么的,还有初始化项目,旦早也会耗费一定的时间

public class MainActivity extends Activity

{

private ListView listView

private DownloadAdapter adapter

private List<String>lists = new ArrayList<String>()

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

for (int i = 0i <10i++)

{

lists.add("button" + i)

}

listView = (ListView) findViewById(R.id.listView1)

adapter = new DownloadAdapter(this, lists)

listView.setAdapter(adapter)

}

}

public class DownloadAdapter extends BaseAdapter

{

private Context context

private List<String>list

private LayoutInflater inflater

public DownloadAdapter(Context context, List<String>list)

{

this.context = context

this.list = list

inflater = LayoutInflater.from(context)

}

@Override

public int getCount()

{

// TODO Auto-generated method stub

return list.size()

}

@Override

public Object getItem(int position)

{

// TODO Auto-generated method stub

return list.get(position)

}

@Override

public long getItemId(int position)

{

// TODO Auto-generated method stub

return list.get(position).hashCode()

}

@Override

public View getView(int position, View convertView, ViewGroup parent)

{

MyHolder myHolder = null

if (convertView == null)

{

convertView = inflater.inflate(R.layout.item, null)

myHolder = new MyHolder()

myHolder.bar = (ProgressBar) convertView

.findViewById(R.id.progressBar1)

myHolder.button = (Button) convertView.findViewById(R.id.button1)

convertView.setTag(myHolder)

}

else

{

myHolder = (MyHolder) convertView.getTag()

}

myHolder.button.setText(list.get(position))

MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,

myHolder.button)

myHolder.button.setOnClickListener(down)

return convertView

}

class MyHolder

{

private ProgressBar bar

private Button button

}

public class MyDownLoadThread extends Thread implements OnClickListener

{

private ProgressBar pBar

private Button btn

private int index = 0

private boolean isStart = false

private Handler handler = new Handler()

{

@Override

public void handleMessage(Message msg)

{

pBar.setProgress(index)

}

}

public MyDownLoadThread(ProgressBar pBar, Button btn)

{

this.pBar = pBar

this.btn = btn

}

@Override

public void onClick(View v)

{

if (!isStart)

{

isStart = true

btn.setText("开始下载")

pBar.setMax(100)

this.start()

}

}

@Override

public void run()

{

while (index <= 100)

{

index++

handler.sendEmptyMessage(0)

try

{

sleep(1000)

}

catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace()

}

if (index >100)

{

btn.setText("下载完成")

break

}

}

}

}

}

简单的demo 仅供参考,希望对你有用


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12254200.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存