简体   繁体   English

无限循环中的服务-Android

[英]Service in infinite loop - android

I am making an app which requires constant querying for a certain attribute in a table.Here is how I am doing it right now.. Code from my service class: 我正在制作一个需要对表中的某个属性进行持续查询的应用。这是我现在的操作方式。. 服务类中的代码:

@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
     ParseUser currentUser = ParseUser.getCurrentUser();
    String username = currentUser.getString("username");

    ParseQuery<ParseUser> query = ParseUser.getQuery();
     query.whereEqualTo("isAttacking", username);
    while(true)
    {
     query.findInBackground(new FindCallback<ParseUser>() {
          public void done(List<ParseUser> objects, ParseException e) {

              if ((e == null)&(objects.size() != 0))
            {
                // The query was successful.

                    ParseUser attacker = objects.get(0);
                    String attackerName = attacker.getUsername();
                    Log.i("ambustest",attackerName);
                    makeToast(attackerName);

            } else {
                Log.i("fd","Something went wrong.");
            }
          }

        });
     return START_STICKY;
    }

}

The query is inside an infinite loop but is executing only once.My best guess is that it breaks when it meets the return statement.Any way to keep the loop running without putting the return statement into unreachable code? 该查询位于无限循环内,但仅执行一次。我的最佳猜测是,它在遇到return语句时会中断。有什么方法可以保持循环运行而不会将return语句放入无法访问的代码中?

try this... 尝试这个...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    ParseUser currentUser = ParseUser.getCurrentUser();
    String username = currentUser.getString("username");

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("isAttacking", username);
    findInBackground();
    return START_STICKY;
}

private void findInBackground() {
    while (true) {
        query.findInBackground(new FindCallback<ParseUser>() {
            public void done(List<ParseUser> objects, ParseException e) {

                if ((e == null) & (objects.size() != 0)) {
                    // The query was successful.

                    ParseUser attacker = objects.get(0);
                    String attackerName = attacker.getUsername();
                    Log.i("ambustest", attackerName);
                    makeToast(attackerName);

                } else {
                    Log.i("fd", "Something went wrong.");
                }
            }
        });
    }
}

It looks like you're getting your threading mixed up. 看起来您正在混淆线程。 Your code attempts to kick off this background process, and then return START_STICKY over and over again. 您的代码将尝试启动此后台进程,然后反复返回START_STICKY
Even if you didn't have to return something, your processes would all be trying to run at the same time. 即使您不必返回任何内容,您的进程也都将尝试同时运行。

The trick would be to call query.findInBackground() from inside the done() method of the FindCallBack , perhaps with some suitable delay term in between queries. 诀窍是从query.findInBackground()done()方法内部调用query.findInBackground() ,也许在FindCallBack查询之间使用一些合适的延迟项。
This way you run one and then when it is finished you kick off the next one. 这样,您可以运行一个,然后在完成时启动下一个。
This would eliminate the entire while loop and thus make it simple to just kick off the first background query and then return START_STICKY 这将消除整个while循环,从而使启动第一个后台查询然后返回START_STICKY变得简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM