简体   繁体   中英

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?

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.
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.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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