我有一个应用程序在其中服务在后台运行,我也使用处理程序。应用程序冻结并需要很长时间来响应,而开放
@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);
}
我的应用程序将运行一个特定的方法来从服务器获取数据并在sqllite数据库中更新它。所以要更新listview与新的db值为此我使用处理程序。所以,当我打开应用程序时,它的打开速度很慢,需要很长时间。有时候应用程序没有响应。滚动列表视图也不平滑哪些Stuck。请帮我实施我所做的是正确的?请帮忙。
解决方案:
在OnCreate中我添加了下面几行:
musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
descAdapter = new DescAdapter(this,musers,this);
contlist.setAdapter(descAdapter);
然后
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);
}
使用上面的代码中,我能够成功地更新列表。但开放时,应用程序仍然很慢。有什么建议么。
对于初学者,您无需检查您的服务是否正在运行。刚开始吧。如果它已经开始,一个新的将不会启动。 –
@ GabeSechan-谢谢你,我将以你说的方式执行。 – user2269164
@GabeSechan thanks..what for stopservice? –