Android 单击按钮并使用其值在内部调用输入接收方法
我试图找到一些解决方案,但没有找到有效的解决方案。我有两个类,一个是sysnctask类,可以下载视频文件,工作正常 //代码Android 单击按钮并使用其值在内部调用输入接收方法,android,android-activity,methods,android-asynctask,Android,Android Activity,Methods,Android Asynctask,我试图找到一些解决方案,但没有找到有效的解决方案。我有两个类,一个是sysnctask类,可以下载视频文件,工作正常 //代码 public class DownloadFileAsync extends AsyncTask<String, String, String> { private Context context; public DownloadFileAsync(Context context) { this.context = context; } @
public class DownloadFileAsync extends AsyncTask<String, String, String> {
private Context context;
public DownloadFileAsync(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
Dominika_details dominika_details= new Dominika_details();
// Output stream
OutputStream output = new FileOutputStream("/sdcard/"+YOUTUBE_ID);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
}
}
I/进程:发送信号。PID:21771信号:9
申请终止
仅在单击按钮时下载此视频文件
将字符串视频
声明为全局变量。并在按钮的onClick()中调用AsyncTask
如何将每个视频id指定为下载的文件名?
将YOUTUBE\u VIDEO\u ID作为AsyncTask参数的参数传递-
我引入了新的DownloadFileAsync(这个,YOUTUBE_ID);是侧击式侦听器,并减少了对其的修改,如DownloadFileAsync(Mainactivity.this,YOUTUBE_ID).excute(video);,当我运行它时,强制关闭此logcat错误@WizardAnd?问题解决了?@F.joel请在您的问题中使用错误logcat更新您的代码,每个人都可以查看!尚未修复@Wizard特别是按钮点击区域感谢@Wizard根据您的建议对我的代码进行了一些修改,无需更改任何代码最终可以正常工作
public class GetYoutubeUrl extends AppCompatActivity {
private static final String YOUTUBE_ID = "3LiubyYpEUk";
private final YouTubeExtractor mExtractor = YouTubeExtractor.create();
private Callback<YouTubeExtractionResult> mExtractionCallback = new Callback<YouTubeExtractionResult>() {
@Override
public void onResponse(Call<YouTubeExtractionResult> call, Response<YouTubeExtractionResult> response) {
bindVideoResult(response.body());
}
@Override
public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) {
onError(t);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_youtube_url);
mExtractor.extract(YOUTUBE_ID).enqueue(mExtractionCallback);
Button download= (Button)findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void onError(Throwable t) {
t.printStackTrace();
Toast.makeText(GetYoutubeUrl.this, "It failed to extract. So sad", Toast.LENGTH_SHORT).show();
}
private void bindVideoResult(YouTubeExtractionResult result) {
/// Here you can get download url link
String video= String.valueOf(result.getSd360VideoUri());
new DownloadFileAsync(this).execute(video);
}
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41ebd9c0)
E/AndroidRuntime: FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:650)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.fredycom.myyoutube.DownloadFileAsync.onPreExecute(DownloadFileAsync.java:41)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.fredycom.myyoutube.GetYoutubeUrl$2.onClick(GetYoutubeUrl.java:43)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DownloadFileAsync(this).execute(video);
}
});
public class DownloadFileAsync extends AsyncTask<String, String, String> {
private Context context;
private String youtubeId;
public DownloadFileAsync(Context context, String youtubeId){
this.context = context;
this.youtubeId = youtubeId; // Use youtubeId as file name
}
.....
}
new DownloadFileAsync(this, YOUTUBE_VIDEO_ID).execute(video);