【问题标题】:app freezes and takes long time to respond while opening应用程序冻结并在打开时需要很长时间才能响应
【发布时间】:2017-02-10 06:01:57
【问题描述】:

我有一个应用程序,其中服务在后台运行,我也在使用处理程序。

@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 数据库中更新它。所以为了这个目的用新的数据库值更新列表视图,我正在使用处理程序。因此,当我打开应用程序时,打开速度非常慢并且需要很长时间。有时应用程序没有响应。滚动列表视图也不流畅,卡住了。请帮助我执行我所做的是否正确?请帮忙。

解决方案:

在 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- 谢谢我会按照你说的方式实施。
  • @GabeSechan 谢谢..stopservice 是什么?
  • @CoDfather 不知道你在问什么。但是一个 stopService 将停止该服务,无论它已启动多少次。因此,只有在您确定需要停止它时才停止它。所以只有在你确定没有其他东西需要它时才调用它。
  • @CoDfather 不,不是。事实上,如果它终止服务,它将返回 true,如果找不到要终止的服务,它将返回 false。

标签: android android-service runnable android-handler


【解决方案1】:

好的,您一直在创建一个新的适配器并对其进行设置。那会破坏你的表现。您需要这样做,以便您的适配器可以更改数据,而不是重新创建。这也意味着您将不需要该 setSelectionFromTop 代码,这也会破坏重复循环的性能。解决这两个问题,你可能会很好。

【讨论】:

  • 你能提供一个你提到的适配器的例子吗?我正在使用新值更新列表视图,所以我使用了这种方式。提前致谢。
  • 一种简单的方法是提供一个函数 setData(List) 该函数将用新列表替换旧列表,然后调用 notifyDataSetChanged();。这足以替换数据。您可以变得更漂亮,但请先尝试。
  • public void SetData(List newlist) { musers.addAll(newlist); this.notifyDataSetChanged();然后在处理程序运行中调用 setData --> musers = (ArrayList) mobstat.listAll(mobstat.class);设置数据(缪斯);但我得到空指针异常
猜你喜欢
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多