jeudi 21 mai 2015

Android - Moveing an Activity inside of background thread

Every time I`m trying to finish an activity inside of a timer method, the activity comes back alive over and over again. I running this activity:

 public class PlayerNoAdmin extends ActionBarActivity {

Timer myTimer; boolean isAdmin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_no_admin);

    Intent oldIntent = getIntent();
    if (oldIntent != null && oldIntent.hasExtra("THE_LIST")){
        songs = oldIntent.getParcelableArrayListExtra("THE_LIST");
        id = oldIntent.getIntExtra("ID",0);
        listId = oldIntent.getIntExtra("LIST_ID",0);
        isAdmin = oldIntent.getBooleanExtra("IS_ADMIN",false);
    }

    //update the list every k seconds
    myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            TimerMethod();
        }

    }, 0, k_time2Update);
}

private void TimerMethod() {
    //This method is called directly by the timer
    //and runs in the same thread as the timer.

    //We call the method that will work with the UI
    //through the runOnUiThread method.
    this.runOnUiThread(Timer_Tick);

}

private Runnable Timer_Tick = new Runnable() {
    public void run() {
        //Here check for update in the list every 30 seconds and send the new location
        String url = getRunUrl();
        new TaskMusicPlay().execute(url);
    }
};

private class TaskMusicPlay extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        String jsonResult = null;
        try {
            String url = params[0];
            TestMain client = new TestMain();
            jsonResult = client.doGetRequest(url);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return  jsonResult;
    }

    @Override
    protected void onPostExecute(String aVoid) {
        super.onPostExecute(aVoid);
        checkIfNew(aVoid);
    }

    private void checkIfNew(String result) {
        try {
            JSONObject object = new JSONObject(result);
            String temp = object.getJSONObject("info").getString("isAdmin");
            isAdmin = (temp.equals("true"));
                if (isAdmin) {
                    Intent intent = new Intent(getApplication(),YouTubePlayer.class);
                    intent.putExtra("THE_LIST", songs);
                    intent.putExtra("ID", id);
                    intent.putExtra("LIST_ID",listId);
                    intent.putExtra("IS_ADMIN",isAdmin);
                    startActivity(intent);
                    finish();
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

At the end, I succeeded to move to the YouTubePlayer activity, but every few seconds the app returns to the code of this activity (and then executes again the startActivity call and goes back to YouTubePlayer) and that's going on and on.

Aucun commentaire:

Enregistrer un commentaire