我有一個應用程序在其中服務在後臺運行,我也使用處理程序。應用程序凍結並需要很長時間來響應,而開放
@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? –