您的位置:首页 > 移动开发 > Android开发

Android关于后台下载前台通知更新进度条的问题

2011-10-20 14:27 851 查看
1.Activity通过接收Service发送过来的进度信息,不断更新进度条

package com.android.border;

import com.android.border.MyService.MyBinder;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.ServiceConnection;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

import android.widget.TextView;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

private static final String RECEIVERTAG = "unRegisterBorder";

private static final String CONNECTSERVICE = "ConnectService";

private static final String DISCONNECTSERVICE = "DisConnectService";

private static final String UNBINDSERVICE = "unbindService";

private MyBorderReceiver receiver;

boolean mBound = false;

private MyService myService;

private int mymsg;

private TextView view;

public class MyBorderReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

mymsg = intent.getIntExtra("msg", 2);

view.setText("广播从服务端接收过来的消息是:" + mymsg);

Message message = new Message();

Bundle data = new Bundle();

data.putInt("progress", mymsg);

message.setData(data);

message.what = 1;

handler.sendMessage(message);

}

}

Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

switch (msg.what) {

case 1:

int progress = msg.getData().getInt("progress");

// Toast.makeText(MainActivity.this,"++++"+progress,Toast.LENGTH_LONG).show();

// 获取通知管理器,用于发送通知

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// 实例化Notification对象,用于添加消息内容

Notification notice = new Notification(R.drawable.icon,

"这是一个通知消息", System.currentTimeMillis());

// //添加扩展的消息标题和内容

// String noticeTitle="通知的标题";

// String noticeContent="通知扩展的内容";

// //通过消息要打开的应用程序

// Intent intent=new Intent("com.fit.demo.notice");

// //等待用户来处理消息

// PendingIntent

// pending=PendingIntent.getActivity(MainActivity.this,0,

// intent, 0);

// //把所有的消息内容放到Notification里

// notice.setLatestEventInfo(getApplicationContext(),

// noticeTitle, noticeContent, pending);

// //打开应用程序后消息图标消失

// notice.flags|=Notification.FLAG_AUTO_CANCEL;

// //通过消息管理器发送消息

// //manager.notify(1,notice);

//

// //用于不断发送消息而不会覆盖以前的消息

// manager.notify(1,notice);

RemoteViews view = new RemoteViews(getPackageName(),

R.layout.notice);

view.setProgressBar(R.id.progress, 100, progress, false);

view.setTextColor(R.id.myview, Color.RED);

view.setTextViewText(R.id.myview, "进度: " + progress + "%");

notice.contentView = view;

Intent intent1 = new Intent("com.fit.demo.notice");

PendingIntent pending = PendingIntent.getActivity(

MainActivity.this, 0, intent1, 0);

notice.contentIntent = pending;

// 打开应用程序后消息图标消失

notice.flags |= Notification.FLAG_AUTO_CANCEL;

// 通过消息管理器发送消息

manager.notify(1, notice);

break;

}

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

view = (TextView) findViewById(R.id.view);

receiver = new MyBorderReceiver();

IntentFilter filter = new IntentFilter();

filter.addAction("com.android.myborder");

this.registerReceiver(receiver, filter);

Button btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

}

});

}

@Override

protected void onStart() {

super.onStart();

Intent intent = new Intent(MainActivity.this, MyService.class);

this.bindService(intent, conn, Context.BIND_AUTO_CREATE);

this.startService(intent);

}

@Override

protected void onResume() {

super.onResume();

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

unregisterReceiver(receiver);

Log.i(RECEIVERTAG, "the receiver have been unRegistered!");

}

@Override

protected void onStop() {

super.onStop();

if (mBound) {

unbindService(conn);

Log.i(UNBINDSERVICE, "the service have been unbinded!");

mBound = false;

}

}

private ServiceConnection conn = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

Log.i(DISCONNECTSERVICE, "service have disConnect!");

mBound = false;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

Log.i(CONNECTSERVICE, "service have connected!");

MyBinder binder = (MyBinder) service;

myService = binder.getService();

mBound = true;

}

};

}

2.Service不断想前台发送进度信息 这里我是用1-100做的示例

package com.android.border;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

public class MyService extends Service {

private final IBinder mBinder=new MyBinder();

private int m=1;

Timer timer=new Timer();

@Override

public IBinder onBind(Intent intent) {

timer.schedule(task,new Date(),500);

return mBinder;

}

class MyBinder extends Binder{

public MyService getService(){

return MyService.this;

}

}

private void sendMsg(){

Intent intent=new Intent("com.android.myborder");

// int m=(int)(Math.random()*10);

if(m<100){

m++;

}

intent.putExtra("msg",m);

sendBroadcast(intent);

}

TimerTask task=new TimerTask() {

@Override

public void run() {

sendMsg();

}

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐