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();
}
};
}
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();
}
};
}
相关文章推荐
- 更新模块通知栏显看不到当前进度,以及更新下载中可以清理通知问题,华为强制更新退出软件后台下载不显示通知问题
- 更新模块通知栏显看不到当前进度,以及更新下载中可以清理通知问题,华为强制更新退出软件后台下载不显示通知问题
- Android下载,在通知栏更新进度
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- android后台下载 进度实时更新
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android APP更新——service后台下载、进度提示、自动安装
- android后台下载多文件切换界面更新问题
- 关于Android Sdk Manager更新下载慢或无法下载问题集合
- 关于android中版本自动更新,从服务器下载下来的文件不能安装的问题
- Android Service 实现下载,实现前台、通知栏同步更新
- android 关于 android sdk manager 更新,下载慢的问题
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新
- android关于UI界面更新的问题
- 一个APP下载升级的Demo(通知栏实时更新下载进度)------(一)
- [置顶] 关于android桌面小组件分开更新的的一些问题
- 关于在aspx前台使用后台变量的问题
- 关于android分支无法下载的问题,revision froyo in manifests not found