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

更多相关文章

  1. Android应用程序键盘(Keyboard)消息处理机制分析(16)
  2. android 随手记 广播通知栏 二
  3. android自定义通知栏并通过广播实现监听
  4. Android学习笔记(1) Android 布局管理器 之 LinearLayout
  5. android短信管理器SmsManager实例详解
  6. Android之MVC——Model通知View去更新(实用)
  7. Android通知Header详解

随机推荐

  1. Handler android~~
  2. Android(安卓)获取应用的安装时间及更新
  3. 对Android(安卓)canvas的一点粗浅理解
  4. Android(安卓)动态权限申请之 shouldShow
  5. Android之——获取手机安装的应用程序
  6. Android:自定义View三个方法的意义
  7. 在电脑上调试手机上的webview
  8. Android中通过scheme实现网页打开App(deep
  9. Android(安卓)绘制圆形进度条
  10. 我的开源项目:Android图片剪裁库