我必须使用intentservice创建一个从服务器下载文件的应用程序,我已经创建了下载服务并将其传递给接收者,但我不知道从哪里开始。结果应该是一个数组字符串,它将通过适配器填充listview。但我不知道如何将结果从接收器传递到适配器所在的主活动。
public class MainActivity extends AppCompatActivity {
ArrayList<PlaylistItem> arrayOfUsers = new ArrayList<PlaylistItem>();
// Create the adapter to convert the array to views
PlaylistAdapter adapter = new PlaylistAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.putExtra("url", "url to be called" );
downloadIntent.putExtra("type","playlist");
downloadIntent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(downloadIntent);
listView.setAdapter(adapter);
}
}
下载服务
public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 1000;
public static final int PLAYLIST_READY = 2000;
private ResultReceiver receiver;
public DownloadService() {
super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
String urlToDownload = intent.getStringExtra("url");
receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
String type= intent.getStringExtra("type");
// se apeleaza ori downloadPlaylist ori downloadMusic
// in functie de ce url am primit in intent
if(type=="playlist")
{
downloadPlaylist(urlToDownload);
}
}
private void downloadPlaylist(String urlToDownload){
String str=null;
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
File musicDirectory = new
File(Environment.getExternalStorageDirectory(),"music");
if (!musicDirectory.exists())
musicDirectory.mkdirs();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(musicDirectory+urlToDownload);
str=input.toString();
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
Bundle resultData = new Bundle();
resultData.putInt("progress" ,(int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
Bundle resultData = new Bundle();
resultData.putInt("progress" ,100);
receiver.send(UPDATE_PROGRESS, resultData);
}
public class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == UPDATE_PROGRESS) {
//send data to Main ACtivity
// update progress bar
}
}
}
我不知道如何从mainactivity中的接收器调用数组,或者反过来调用数组来填充它。如果你能给我举个例子。谢谢您!
暂无答案!
目前还没有任何答案,快来回答吧!