2017-02-10 60 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

相关问题