Activity与Service通过广播交换复杂对象数据用法详解
2011-10-30 19:40
393 查看
最近学习新浪微博开放平台,实现了一个应用,通过后台Service监控微博数据,发现数据更新后通知前台程序,并将博客数据列表发送给前台Activity。
其中利用BroadcastReceiver对象分别在Activity和Service注册了一个广播,通过发送不同的广播控制前台和后台的数据交换,并通过Serializable对象传递复杂的自定义对象类型给Activity。
程序片段如下:后台监控weibo Service
//继承自Service的子类
复制代码
AndriodFocusMe 主UI界面Activity
public class AndriodFocusMe extends Activity implements Runnable{
/** Called when the activity is first created. */
DataReceiver dataReceiver;//BroadcastReceiver对象
ProgressBar progressbar;
ListView listview;
int CMD_STOP_SERVICE = 0;
int CMD_RESET_SERVICE = 1;
int CMD_GET_WEIBO_DATA = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weibodataview);
Button beginOuathBtn= (Button) findViewById(R.id.Button_WeiBo);
Button endBtn = (Button) findViewById(R.id.flashData);
listview = (ListView)findViewById(R.id.weibodatalist);
progressbar = (ProgressBar)findViewById(R.id.wbprogressbar);
progressbar.setVisibility(View.INVISIBLE);
beginOuathBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick( View v )
{
Intent myIntent = new Intent(AndriodFocusMe.this, WeiboService.class);
AndriodFocusMe.this.startService(myIntent);//发送Intent启动Service
progressbar.setVisibility(View.VISIBLE);
}
} );
endBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick( View v )
{
Intent intent = new Intent();//创建Intent对象
intent.setAction("weibo4andriod.focusme.weiboService");
intent.putExtra("cmd", CMD_GET_WEIBO_DATA);
sendBroadcast(intent);//发送广播
}
} );
}
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
ArrayList<HashMap<String, String>> weibodatalist;
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
try {
Bundle bundle = intent.getExtras();
//反序列化,在本地重建数据
Serializable data = bundle.getSerializable("weibodata");
if (data != null) {
weibodatalist = (ArrayList<HashMap<String, String>>)data;
SimpleAdapter mSchedule = new SimpleAdapter(AndriodFocusMe.this,weibodatalist,
R.layout.weibodata_itemview,
new String[] {"CreatedAt", "WeiBoText"},
new int[] {R.id.title,R.id.weibotext});
listview.setAdapter(mSchedule);
} else {
return;
}
} catch (Exception e) {
Log.v("test", e.toString());
}
progressbar.setVisibility(View.GONE);
}
}
@Override
protected void onStart() {//重写onStart方法
//注册用于接收Service传送的广播
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("weiboDataChanged");
registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
NotificationManager m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m_NotificationManager.cancel(R.id.TextView01);
super.onStart();
}
@Override
protected void onStop() {//重写onStop方法
unregisterReceiver(dataReceiver);
finish();
super.onStop();
}
}
复制代码
通过这个例子,基本可以掌握Android常用组件的用法,希望能给各位读者带来帮助,并欢迎提出宝贵意见。
其中利用BroadcastReceiver对象分别在Activity和Service注册了一个广播,通过发送不同的广播控制前台和后台的数据交换,并通过Serializable对象传递复杂的自定义对象类型给Activity。
程序片段如下:后台监控weibo Service
//继承自Service的子类
复制代码
AndriodFocusMe 主UI界面Activity
public class AndriodFocusMe extends Activity implements Runnable{
/** Called when the activity is first created. */
DataReceiver dataReceiver;//BroadcastReceiver对象
ProgressBar progressbar;
ListView listview;
int CMD_STOP_SERVICE = 0;
int CMD_RESET_SERVICE = 1;
int CMD_GET_WEIBO_DATA = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weibodataview);
Button beginOuathBtn= (Button) findViewById(R.id.Button_WeiBo);
Button endBtn = (Button) findViewById(R.id.flashData);
listview = (ListView)findViewById(R.id.weibodatalist);
progressbar = (ProgressBar)findViewById(R.id.wbprogressbar);
progressbar.setVisibility(View.INVISIBLE);
beginOuathBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick( View v )
{
Intent myIntent = new Intent(AndriodFocusMe.this, WeiboService.class);
AndriodFocusMe.this.startService(myIntent);//发送Intent启动Service
progressbar.setVisibility(View.VISIBLE);
}
} );
endBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick( View v )
{
Intent intent = new Intent();//创建Intent对象
intent.setAction("weibo4andriod.focusme.weiboService");
intent.putExtra("cmd", CMD_GET_WEIBO_DATA);
sendBroadcast(intent);//发送广播
}
} );
}
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
ArrayList<HashMap<String, String>> weibodatalist;
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
try {
Bundle bundle = intent.getExtras();
//反序列化,在本地重建数据
Serializable data = bundle.getSerializable("weibodata");
if (data != null) {
weibodatalist = (ArrayList<HashMap<String, String>>)data;
SimpleAdapter mSchedule = new SimpleAdapter(AndriodFocusMe.this,weibodatalist,
R.layout.weibodata_itemview,
new String[] {"CreatedAt", "WeiBoText"},
new int[] {R.id.title,R.id.weibotext});
listview.setAdapter(mSchedule);
} else {
return;
}
} catch (Exception e) {
Log.v("test", e.toString());
}
progressbar.setVisibility(View.GONE);
}
}
@Override
protected void onStart() {//重写onStart方法
//注册用于接收Service传送的广播
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("weiboDataChanged");
registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
NotificationManager m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m_NotificationManager.cancel(R.id.TextView01);
super.onStart();
}
@Override
protected void onStop() {//重写onStop方法
unregisterReceiver(dataReceiver);
finish();
super.onStop();
}
}
复制代码
通过这个例子,基本可以掌握Android常用组件的用法,希望能给各位读者带来帮助,并欢迎提出宝贵意见。
相关文章推荐
- Activity与Service通过广播交换复杂对象数据用法详解
- Activity与Service通过广播交换复杂对象数据用法详解
- Activity与Service通过广播交换复杂对象数据用法详解
- Activity与Service通过广播交换复杂对象数据用法详解
- Activity与Service通过广播交换复杂对象数据用法详解
- Android中Activity与Service如何通过广播交换复杂对象数据
- Android中Activity与Service如何通过广播交换复杂对象数据
- Android中通过广播交换复杂对象数据
- activity与service通过serializable传递复杂对象
- Android四大组件之Service 远程服务 通过AIDL进行进程间复杂类型数据交换
- Fragment和Activity之间通过广播的方式传递数据
- 通过广播实现简单数据交换前后台数据交换BroadcastReceiver
- Android Service与Activity之间通信的几种方式(1)---通过broadcast(广播)的形式
- Android通过Parcelable传递复杂的对象数据
- JSON实现Android 网络数据交换(包括复杂对象的传送)
- Activity之间的数据交换(简单的数据,类对象,图片)
- MainActivity 通过广播接收 IntentService传过来的数据
- Fragment和Activity之间通过广播的方式传递数据
- 【数据】Activity数据返回——startActivityForResult()用法详解
- Android Service与Activity之间通信的几种方式(1)-----通过Binder对象