下载完成后 ui 应该需要转换为可点击的项目然后当用户点击它时用户可以打开文件。
这是我的 HomeFragment.java
package com.hackerindia.movies;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;
import android.widget.ProgressBar;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.material.snackbar.Snackbar;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import android.app.DownloadManager;
import android.content.Context;
import android.database.Cursor;
public class HomeFragment extends Fragment {
private LinearLayout movieContainer;
private RequestQueue requestQueue;
private ProgressBar progressBar;
private TextView statusTextView;
private final String url = "https://example.com/App/data.json";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
movieContainer = view.findViewById(R.id.movie_container);
//errorTextView = view.findViewById(R.id.error_text);
requestQueue = Volley.newRequestQueue(requireContext());
// Fetch data from JSON endpoint using Volley
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray dataArray = response.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
JSONObject movieObject = dataArray.getJSONObject(i);
String movieTame = movieObject.getString("movie_name");
boolean isPlaying = Boolean.parseBoolean(movieObject.getString("is_playing"));
if (isPlaying) {
// Append movie poster, title, and two buttons if is_playing is true
String movieName = movieObject.getString("movie_name");
String moviePosterUrl = movieObject.getString("movie_poster");
String downloadLink = movieObject.getString("download_link");
String movieSrc = movieObject.getString("movie_src");
// Create movie item view
View movieItemView = inflater.inflate(R.layout.movie_item, null);
ImageView moviePoster = movieItemView.findViewById(R.id.movie_poster);
TextView movieTitle = movieItemView.findViewById(R.id.movie_title);
Button downloadButton = movieItemView.findViewById(R.id.download_button);
Button watchOnlineButton = movieItemView.findViewById(R.id.watch_online_button);
// Set movie poster and title
Glide.with(requireContext()).load(moviePosterUrl).into(moviePoster);
movieTitle.setText(movieName);
// Set download link and movie source as tags for buttons
downloadButton.setTag(downloadLink);
watchOnlineButton.setTag(movieSrc);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get download link
String downloadLink = (String) v.getTag();
}
});
watchOnlineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String movieSrc = (String) v.getTag();
Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
intent.putExtra("movieSrc", movieSrc);
startActivity(intent);
}
});
// Add movie item view to movie container
movieContainer.addView(movieItemView);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(jsonObjectRequest);
return view;
}
}
这是我的 DownloadsFragment.java
package com.hackerindia.movies;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.hackerindia.movies.databinding.FragmentDownloadsBinding;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadsFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_downloads, container, false);
return view;
}
}