Vote count:
0
Hi I am working with android.I am looking to update ui from service data. So that I get data from service via broadcast receiver succesfully and I updated listview when value changes.My problem is that during activity running, if i minimise the activity and then maximise, the another receiver is get registered and which will repeats my listview data.How can I set only one receiver when after maximise ? here is my code
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);
}
}
}
asked 1 min ago
Android broadcast receiver get registered multiple times when activity minimise and maximise
Aucun commentaire:
Enregistrer un commentaire