Reputation: 1105
I have an app in which Service is running in background also i am using Handler.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seconds);
if(!isMyServiceRunning(serv.class))
{
startService(new Intent(this, serv.class));
} else {
Log.e("Shiva","Service already running");
}
status = (EditText)findViewById(R.id.status);
btn_send = (ImageButton) findViewById(R.id.btn_send);
btn_send.setOnClickListener(this);
contlist = (ListView)findViewById(R.id.contlist);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String y = prefs.getString("mobstat",null);
status.setText(y);
status.setSelection(status.getText().length());
if(!prefs.getBoolean("firstTime", false))
{
try
{
getNumber(seconds.this.getContentResolver());
} catch (JSONException e)
{
e.printStackTrace();
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.apply();
}
nonstoprun();
}
private boolean isMyServiceRunning(Class<?> serviceClass)
{
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if (serviceClass.getName().equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(update);
Log.e("Shiva","Handler Stopped");
}
@Override
protected void onResume() {
super.onResume();
nonstoprun();
Log.e("Shiva","Handler Started");
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(update);
}
private void nonstoprun()
{
handler = new Handler();
update = new Runnable()
{
@Override
public void run()
{
if(!isRunning) {
isRunning = true;
musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
descAdapter = new DescAdapter(seconds.this, musers, seconds.this);
int index = contlist.getFirstVisiblePosition();
View v = contlist.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - contlist.getPaddingTop());
contlist.setAdapter(descAdapter);
contlist.setSelectionFromTop(index, top);
handler.postDelayed(this, 1000);
} else {
isRunning = false;
}
}
};
handler.postDelayed(update, 10);
}
My app will run a specific method to fetch the data from server and update it in the sqllite db. So to update listview with new db values for this purpose i am using handler. So when ever i opened the app it is very slow in opening and takes long time. Sometimes app wasn't not responding. Scrolling listview also not smooth which stucks. Please help me that implementation what i did is correct? Please help.
Solution:
In Oncreate i added the below lines:
musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
descAdapter = new DescAdapter(this,musers,this);
contlist.setAdapter(descAdapter);
Then
private void nonstoprun()
{
update = new Runnable()
{
@Override
public void run()
{
ArrayList<mobstat> musers1 = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
setData(musers1);
handler.postDelayed(this, 1000);
}
private void setData(ArrayList<mobstat> musers1)
{
musers.clear();
musers.addAll(musers1);
descAdapter.notifyDataSetChanged();
}
};
handler.postDelayed(update, 10);
}
Using the above code i am able to update list successfully. But still app is slow while opening. Any suggestions.
Upvotes: 0
Views: 92
Reputation: 93614
Ok, you're constantly creating a new adapter and setting it. That will destroy your performance. You need to make it so that your adapter can change data, rather than being recreated. That will also mean you won't need that setSelectionFromTop code, which will also kill performance being repeatedly looped. Fix those two things and you'll probably be good.
Upvotes: 1