您好我正在使用android.I我希望从服务数据更新ui。因此,我通过广播接收器成功获得服务数据,并在价值变化时更新了listview。我的问题是在活动运行期间,如果我最小化活动然后最大化,另一个接收器将被注册,并将重复我的列表视图数据。如何在最大化后设置一个接收器?这是我的代码
public class MainActivity extends Activity {
ListView l1;
ArrayList<String> t1=new ArrayList<String>();
ArrayList<String> d1=new ArrayList<String>();
ArrayList<Integer> i1=new ArrayList<Integer>();
MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(ListView)findViewById(R.id.listView1);
//l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);
//Start our own service
Intent intent = new Intent(MainActivity.this,
com.example.androidservice.MyService.class);
startService(intent);
super.onStart();
}
@Override
protected void onStop() {
unregisterReceiver(myReceiver);
stopService(new Intent(MainActivity.this,MyService.class));
super.onStop();
}
@Override
protected void onPause() {
//unregisterReceiver(myReceiver);
stopService(new Intent(MainActivity.this,MyService.class));
super.onPause();
}
private class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
String datapassed1 = arg1.getStringExtra("DATAPASSED1");
String datapassed2 = arg1.getStringExtra("DATAPASSED2");
Toast.makeText(MainActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed1),
Toast.LENGTH_SHORT).show();
t1.add(datapassed1);
d1.add(datapassed2);
for(int i=0;i<t1.size();i++)
{
i1.add(R.drawable.ic_launcher);
}
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
}
class dataListAdapter extends BaseAdapter {
ArrayList<String> Title, Detail;
ArrayList<Integer> imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(ArrayList text, ArrayList text1,ArrayList text3) {
notifyDataSetChanged();
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title.get(position));
detail.setText(Detail.get(position));
i1.setImageResource(imge.get(position));
return (row);
}
}
}
答案 0 :(得分:0)
注册广播接收器并在onCreate()中启动服务 un注册接收器并在onDestroy()中停止服务。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(ListView)findViewById(R.id.listView1);
//l1.setAdapter(new dataListAdapter(t1,d1,i1));
//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);
//Start our own service
Intent intent = new Intent(MainActivity.this,
com.example.androidservice.MyService.class);
startService(intent);
}
@Override
protected void onDestroy() {
unregisterReceiver(myReceiver);
stopService(new Intent(MainActivity.this,MyService.class));
super.onDestroy();
}
答案 1 :(得分:0)
您应该在BroadcastReceiver
中注册onResume
并在onPause
中取消注册。之后,您不需要在任何其他活动的生命周期方法中注册/取消注册。
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, new IntentFilter(MyService.MY_ACTION));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
我希望这会有所帮助。
答案 2 :(得分:0)
这里应该有一些东西(也许是你的清单)。对我来说,如何处理你的接收器似乎是正确的(你不必使用onPause-onResume来注册/取消注册)。
请确保您也不要在清单文件中注册接收器。