主要是用于在Service和Activity中的通讯
实现音乐播放进度的UI跟新
逻辑:
在Service中发送广播
1 sendBroadcast(intent);
Intent intent = new Intent("注册的Action");
可以向Intent中传键值对
在Activity中接收和创建广播;
因为需要在广播的方法中更新IU 所以在Activity中使用内部类构建广播
在onStart()中注册广播
创建广播类
1 创建广播内部类
class SBC extends BroadcastReceiver{
}
2 在抽象方法中更新UI
public viod onReceive(Context context,Intent intent){
}
3 根据发送广播时 传入的参数Intent更新UI
int current = intent.getIntExtra(键,值)
更新UI
tv.setText(current);
在onStart()方法中注册广播
1 创建广播类对象
SBC sbc = new SBC();
2 创建意图过滤器对象
IntentFilter filter = new IntentFilter();
3 向意图过滤器对象添加Action
filter.addAction("Action")
4 注册广播
this.registerRcceiver(sbc,filter2);
在onStop()方法中关闭广播
this.unresifterReceiver(sbc)
源码:
UI更新线程:
package com.example.d08broadcast;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
SeekBar sb;
TextView tv1;
TextView tv2;
private TextView tv;
BC bc;
private SBC sbc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
sb = (SeekBar) findViewById(R.id.seekBar1);
tv1 = (TextView) findViewById(R.id.textView2);
tv2 = (TextView) findViewById(R.id.textView3);
}
public void doClick(View v){
switch (v.getId()) {
case R.id.button2:
Intent intent1 = new Intent(this,ss.class);
this.startService(intent1);
break;
}
}
class SBC extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int current = intent.getIntExtra("current",0);
int total = intent.getIntExtra("total",0);
sb.setMax(total);
sb.setProgress(current);
SimpleDateFormat fmt = new SimpleDateFormat("mm:ss");
tv1.setText(fmt.format(new Date(current*1000)));
tv2.setText(fmt.format(new Date(total*1000)));
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
sbc = new SBC();
IntentFilter filter2 = new IntentFilter();
filter2.addAction("UPDATE_PROGRESS");
this.registerReceiver(sbc, filter2);
}
@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(sbc);
}
}
服务线程:
package com.example.d08broadcast;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ss extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
// TODO Auto-generated method stub
new Thread(){
public void run() {
int current = 0;
int total = 200;
for(current=0;current<total;current++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent("UPDATE_PROGRESS");
intent.putExtra("current", current);
intent.putExtra("total", total);
sendBroadcast(intent);
}
stopSelf(startId);
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="doClick"
android:text="查看当前时间" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="32dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBar1"
android:text="current"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBar1"
android:onClick="doClick"
android:layout_centerHorizontal="true"
android:text="播放" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/seekBar1"
android:layout_below="@+id/seekBar1"
android:text="总时间"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>