Android -在后台线程中移动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();
            }
        }
    }
}

最后,我成功地移动到YouTubePlayer活动,但每隔几秒钟,应用程序返回到该活动的代码(然后再次执行startActivity调用并返回到YouTubePlayer),这是持续不断的

你的计时器周期性地一次又一次地召唤玩家开始。

你必须使一个cancel()调用定时器,如果它不再需要,这样你就可以防止它为你的活动持有一个引用,从而防止从回栈和GC中删除。

http://developer.android.com/reference/java/util/Timer.html

和你的定时器是不运行在同一线程,因为它的代码,因为定时器线程是另一个线程和定时器的代码在UI上运行。您可以通过在runOnUIThread()之外和它内部的计时器的运行方法中添加一些日志来检查它。

相关内容

  • 没有找到相关文章

最新更新