onBackPressed和android中的线程
问题描述:
我有一个线程在我的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(); } } }
答
任何想法?
停止线程。 Android不处理你自己分叉的线程。如果你分叉它,你停止它。
正好如何停止线程取决于线程正在做什么,这是你拒绝在这里解释,所以我不能给你具体的建议。
+0
我想要,但是将'myThread.stop();`放在我的`onBackPressed()`中不起作用。它回来了,但没有执行任何指令... – 2011-02-18 14:57:26
我的权利是什么问题不是与停止线程,但有理由为什么onBackPressed不被调用? – Olegas 2011-02-18 14:59:07
就是这样!我在Android 2.1上。它在我的onBackPressed()方法中进行,但只有当线程完成时...但是如果我按下按钮,它仍然会返回!但是没有执行我的代码 – 2011-02-18 15:50:31