我有一個線程在我的ListActivity
中運行,有時需要太長時間才能從互聯網獲取數據,所以用戶可能想要退出並返回。onBackPressed和android中的線程
但是,當我按下BACK按鈕時,它顯示前一個視圖,但線程仍然保持執行狀態。
我重寫onBackPressed()
方法,並設置breakpointm但在調試時我看到,它不經過它,所以我不知道還能做什麼?
任何想法?
謝謝
這是我的代碼。基本上,我不知道爲什麼它,即使它可以追溯到以前的活動當y按BACK按鈕不執行onKeyPressed()
... 代碼編輯
// package + imports public class ListaVideosActivity extends ListActivity implements Runnable { private ProgressDialog pd; private Context mContext; private View header; private View footer; private Vibrator vibrator; private boolean firsTime = true; private Thread thread; private Long playlistId; private String playlistTitle; protected int headerDrawable; private Playlist pList; public static final String TAG_PL_ID = "id_playlist"; public static final String TAG_PL_TITLE = "title_playlist"; public static final String TAG_PL_HEADER = "header_playlist"; private SensorManager mSensorManager; private ShakeListener mShakeListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Context es el MainTabActivity mContext = getParent(); MainTabActivity.tabHost.getTabWidget().setVisibility(View.GONE); playlistId = getIntent().getLongExtra(TAG_PL_ID, 0L); playlistTitle = getIntent().getStringExtra(TAG_PL_TITLE); headerDrawable = getIntent().getIntExtra(TAG_PL_HEADER, 0); setLoadingView(); thread = new Thread(this); thread.start(); createListHeader(); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); mShakeListener = new ShakeListener(mSensorManager); } @Override protected void onPause() { mSensorManager.unregisterListener(mShakeListener); super.onPause(); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(mShakeListener, mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); } private void setLoadingView() { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.videos_loading_dialog, null); MainTabActivity.tabHost.getTabWidget().setVisibility(View.GONE); setContentView(layout); } private void createListHeader() { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(LAYOUT_INFLATER_SERVICE); header = inflater.inflate(R.layout.videos_list_header, null); footer = inflater.inflate(R.layout.screen_footer, null); ((ImageView) header).setImageResource(headerDrawable); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { Video video = (Video) l.getItemAtPosition(position); Intent intent = new Intent(mContext, BCPlayerActivity.class); BCPlayerActivity.video = video; mContext.startActivity(intent); } public void run() { pList = BCVideoGetter.getPlaylistById(playlistId); if (pList != null) { // hay lista handler.sendEmptyMessage(1); } else { handler.sendEmptyMessage(0); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: Toast.makeText(mContext, R.string.noHostError, Toast.LENGTH_LONG).show(); case 1: if (firsTime) { // Shake mShakeListener.setForceThreshHold(1.9); mShakeListener .setOnShakeListener(new ShakeListener.OnShakeListener() { @Override public void onShake() { if (pd == null || !pd.isShowing()) { vibrator.vibrate(300); updateVideosList(); } } }); MainTabActivity.tabHost.getTabWidget().setVisibility( View.VISIBLE); setContentView(R.layout.videos_list); getListView().addHeaderView(header, null, false); getListView().addFooterView(footer, null, false); setListAdapter(new VideosListAdapter(mContext, R.layout.videos_list_item, (ArrayList) pList .getVideos())); vibrator = ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)); firsTime = false; } else { ((VideosListAdapter) getListAdapter()).setItems((ArrayList) pList.getVideos()); ((VideosListAdapter) getListAdapter()) .notifyDataSetChanged(); } if (pd != null) pd.dismiss(); break; } } }; @Override public void onBackPressed() { mSensorManager.unregisterListener(mShakeListener); thread.interrupt(); PestanaProgramasTVActivity.group.back(); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.news_list_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.update: updateVideosList(); break; case R.id.settings: Intent settingsActivity = new Intent(mContext, SettingsActivity.class); startActivity(settingsActivity); break; } return true; } private void updateVideosList() { if (pd == null || !pd.isShowing()) { pd = ProgressDialog.show(mContext, "", getResources().getString( R.string.news_update), true); thread = new Thread(this); thread.start(); } } }
我的權利是什麼問題不是與停止線程,但有理由爲什麼onBackPressed不被調用? – Olegas 2011-02-18 14:59:07
就是這樣!我在Android 2.1上。它在我的onBackPressed()方法中進行,但只有當線程完成時...但是如果我按下按鈕,它仍然會返回!但是沒有執行我的代碼 – 2011-02-18 15:50:31