在Android开发中,我们经常需要利用Fragment来动态更新UI,我们利用Service进行联网,从网上下载下来的数据通过广播出去,再利用Receiver接受,下面是例子:
这是service的写法:
public class InternetService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
@Override
public void onCreate() {
lm = new LogicMethod(this);
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
if (method.equals("login")) {
final String username = bundle.getString("username");
final String password = bundle.getString("password");
new Thread() {
@Override
public void run() {
try {
String SendData = null;
SendData = xmlhead + "<request><method>" + method
+ "</method><username>" + username
+ "</username><password>" + password
+ "</password></request>";
String st = nu.XHttp(SendData);
if (null != st) {
BroadCastMethod(st, "com.onpiece.sslogin");
} else {
BroadCastMethod("connection fail!",
"com.onpiece.sslogin");
Log.i(TAG, "st is null");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
else {
}
}
private boolean BroadCastMethod(String st, String ReceiveAction) {
Intent intentReceiver = new Intent();
intentReceiver.setAction(ReceiveAction);
// intentReceiver.putExtra("name", "sucess\r\n");
Log.i(TAG, "st is " + st);
if (null == st) {
intentReceiver.putExtra("name", "");
} else {
intentReceiver.putExtra("name", st);
}
intentReceiver.putExtra("networkstat", NetworkStat);
intentReceiver.putExtra("netavailablestat", NetworkAvailableStat);
InternetService.this.sendBroadcast(intentReceiver);
Log.i(TAG, "发送广播成功");
return true;
}
@Override
public void onDestroy() {
System.out.println("----onDestory-----");
super.onDestroy();
}
}
public class ContactsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View contactsLayout = inflater.inflate(R.layout.contacts_layout,
container, false);
CFcontext = this;
mListView = (ListView) contactsLayout
.findViewById(R.id.contact_name_listview);
return contactsLayout;
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "in onReceive");
String return_data = intent.getExtras().getString("name");
if (return_data != null && return_data != "") {
XmlParser xpm = new XmlParser();
String status = xpm.XMLParseMethod(return_data, "status");
if (status.equals("0")) {
String username = xpm.XMLParseMethod(return_data,
"username");
Log.i(TAG, "username is " + username);
String usersex = xpm.XMLParseMethod(return_data, "usersex");
List<String> items = new ArrayList<String>();
items.add(username);
i++;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
CFcontext.getActivity(),
android.R.layout.simple_list_item_1, items);
mListView.setAdapter(adapter);
} else if (status.equals("getcontactname")) {
String outcome = xpm.XMLParseMethod(return_data, "outcome");
if (outcome.equals("connection fail!")) {
Log.i(TAG, "嘿嘿,联网失败");
Toast.makeText(CFcontext.getActivity(), "嘿嘿,联网失败",
Toast.LENGTH_SHORT).show();
}
}
}
}
}
@Override
public void onAttach(Activity activity) {
Log.i(TAG, "in onAttach");
/** 注册广播 */
receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.onpiece.ContactsFragment"); // 只有持有相同的action的接受者才能接收此广播
activity.registerReceiver(receiver, filter);
super.onAttach(activity);
}
}
不能用这种方法接收广播,如下:
// if (null == receiver) {
// receiver = new MyReceiver();
// }
// LocalBroadcastManager broadcastManager = LocalBroadcastManager
// .getInstance(getActivity());
// IntentFilter filter = new IntentFilter();
// // filter.addCategory(Intent.CATEGORY_DEFAULT);
// filter.addAction("com.onpiece.ContactsFragment");
// broadcastManager.registerReceiver(receiver, filter);
利用这种方法接收不到广播。原因现在还不知道。。。