2017-02-10 58 views
0

我有一個應用程序在其中服務在後臺運行,我也使用處理程序。應用程序凍結並需要很長時間來響應,而開放

@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); 
} 

使用上面的代碼中,我能夠成功地更新列表。但開放時,應用程序仍然很慢。有什麼建議麼。

+2

對於初學者,您無需檢查您的服務是否正在運行。剛開始吧。如果它已經開始,一個新的將不會啓動。 –

+0

@ GabeSechan-謝謝你,我將以你說的方式執行。 – user2269164

+0

@GabeSechan thanks..what for stopservice? –

回答

1

好的,你不斷創建一個新的適配器並設置它。這會破壞你的表現。您需要做到這一點,以便您的適配器可以更改數據,而不是重新創建。這也意味着你不會需要setSelectionFromTop代碼,這也會導致性能重複循環。解決這兩件事,你可能會很好。

+0

你能提供一個適配器的例子嗎?我正在用新值更新列表視圖,所以我用這種方式。提前致謝。 – user2269164

+1

一個簡單的方法是提供一個函數setData(List​​)該函數將用新函數替換舊列表,然後調用notifyDataSetChanged();.這足以替代數據。 YOu可以更有魅力,但先嚐試一下。 –

+0

public void SetData(List newlist){ musers.addAll(newlist); this.notifyDataSetChanged(); }然後調用setData內部處理程序運行 - > musers =(ArrayList )mobstat.listAll(mobstat.class);使用setData(musers);但我得到了空指針異常 – user2269164

相關問題