Android - 在后台线程中移动 Activity

Android - Moveing an Activity inside of background thread

每次我尝试在计时器方法中完成 activity 时,activity 一遍又一遍地活着。 我 运行 这个 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 activity,但每隔几秒应用 returns 到这个 activity 的代码(然后再次执行 startActivity调用并返回到 YouTubePlayer),并且一直在继续。

您的计时器会定期调用播放器重新开始。

如果不再需要 Timer,您必须对其进行 cancel() 调用,以防止它为您的 activity 保留引用,从而防止从后台堆栈和 GC 中删除。

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

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