Android 启动新意图时保持线程运行
事情是这样的,当我启动一个意图(播放正在下载的内容的媒体播放器)时,我想让我的线程保持运行(它正在下载一个文件) 问题是,当我调用intent时,onPause方法会自动调用 那么我应该怎么做来避免这种情况呢Android 启动新意图时保持线程运行,android,android-intent,Android,Android Intent,事情是这样的,当我启动一个意图(播放正在下载的内容的媒体播放器)时,我想让我的线程保持运行(它正在下载一个文件) 问题是,当我调用intent时,onPause方法会自动调用 那么我应该怎么做来避免这种情况呢 public class MyActivity extends Activity { MyThread my_thread; public void onCreate(Bundle icicle) { super.onCreate(ici
public class MyActivity extends Activity {
MyThread my_thread;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//Starting My thread
my_thread = new MyThread();
my_thread.start();
// Starting the intent
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "video/*");
startActivity(intent);
@Override
protected void onPause() {
super.onPause();
// Stop my thread
my_thread.interrupt();
}
}
编辑:我正在下载一个实时流,因此除非用户决定关闭Activity,否则它不会停止。当您启动一个新活动时,总是调用类扩展活动的OnPause事件,这就是您使用startActivity(intent)所做的 如果希望线程在退出应用程序时停止下载数据,我认为这就是myThread.interrupt()进入OnPause回调的含义,不要使用“OnPause”,而是选择另一个事件。您可以在此处找到启动事件的详细说明:您可以尝试以下方法之一:
IntentService
(即在串行后台线程上执行其工作),下载该文件,完成后,您将发送一个广播意图,其中包含可找到该文件的路径。这里有两篇文章可以帮助您使用IntentServices
(和)异步任务
BroadcastReceiver
,以处理上述意图。比如:
public class MainActivity extends Activity {
protected BroadcastReceiver myDownloadCompleteReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("my_package.actions.DOWNLOAD_COMPLETE".equals(intent.getAction())) {
String filePath = intent.getStringExtra("DOWNLOAD_PATH");
// use the filePath
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("my_package.actions.DOWNLOAD_COMPLETE");
LocalBroadcastManager.getInstance(this).registerReceiver(myDownloadCompleteReceiver, filter);
}
}
您可能想看看AsyncTasks,它们允许在Android上为这类事情提供更好的多线程 下面是一个简单的例子:
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
编辑:没有看到它是一个连续的流,不要使用上面的代码。也许你应该把它放在你的原始问题中?在开始线程之前,用
isAlive()
检查它,然后使用.join()
,这样当你开始新的意图时下载就完成了。它是一个实时流,所以没有结尾。无论如何,Activity
的类名是什么作为MediaPlayer的代码,我必须看看如果线程没有完成它,它如何解释未关闭(未完成)的文件。我也需要MyThread。线程下载文件,Vlc擅长播放不断变化的文件大小,但问题是自动调用intent会破坏我的线程。除非删除interrupt()方法并在意图关闭时调用它,否则可能…如果我在doInBackground中设置(mythread.)start方法的代码,并在我的主要活动中调用新的意图,异步任务会被销毁吗?否,如果要在OnPostExecute中使用回调,请确保它知道要调用哪个活动。我正在考虑这样做。1) DownloadFilesTask download=新建DownloadFilesTask();下载.execute();2) 通过意图调用媒体播放器。请记住,它是一个连续流,因此我认为不会调用onPostExecute。如果它是一个连续流,请不要使用AsyncTask,它将占用太多的系统资源,因为它不会被调用到其他任务。改用DownloadManager。
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}