在我的项目中,我必须在文件下载完成后更新适配器。 我已经创建了一个下载文件的类。我使用了一个广播接收器,它在文件下载完成时执行。我用来下载文件的代码如下。
private final BroadcastReceiver onComplete = new BroadcastReceiver()
{
public void onReceive(Context ctxt, Intent intent)
{
//buttonText.setText("");
// GlobalVariable.downloading_pos.add()
GlobalVariable.downloadingFlag=1;
GlobalVariable.downloading_started=0;
GlobalVariable.downloading_pos.clear();
Log.d("Size==", "onReceive: "+GlobalVariable.downloading_pos.size());
Log.d(TAG, "onReceive: "+ GlobalVariable.downloading_started);
// GlobalVariable.downloaded_position.put(downloaded_position,downloaded_position);
// GlobalVariable.download_AudioID.put(audio_file, audio_file);
// GlobalVariable.download_AudioID.put(audio_file, audio_file);
Log.d("dsvvv==", "onReceive: " + downloadFileName.split("\\.")[0]);
// GlobalVariable.download_file.add(downloadFileName.split("\\.")[0]);
GlobalVariable.download_file.add(downloadFileName.split("\\.")[0]);
if (!apkStorage.isFile()) {
buttonText.setEnabled(false);
// z myBookingListAdapter.notifyDataSetChanged();
buttonText.setText("Audio Downloaded");
buttonText.setBackgroundResource(R.drawable.white_button_mybooking);
buttonPlay.setBackgroundResource(R.drawable.orange_button_mybooking);
}
else
{
buttonText.setText(R.string.downloadFailed);//If download failed change button text
buttonText.setEnabled(true);
buttonText.setText(R.string.downloadAgain);//Change button tex
}
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = downloadManager.query(q);
if (c.moveToFirst())
{
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL)
{
// process download
//String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
// get other required data by changing the constant passed to getColumnIndex
Uri uri = downloadManager.getUriForDownloadedFile(referenceId);
if (from_flag.equals(ctxt.getString(R.string.my_booking)))
{
// Fragdash_mybooking.adapterReset();
GlobalVariable.download_AudioID.put(downloadFileName.split("\\.")[0], downloadFileName.split("\\.")[0]);
}
else if (from_flag.equals(ctxt.getString(R.string.my_invitation)))
{
// Fragdash_invitation.adapterReset();
GlobalVariable.download_AudioID.put(downloadFileName.split("\\.")[0], downloadFileName.split("\\.")[0]);
}
}
}
c.close();
Log.e("INSIDE", "" + referenceId);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle("Ucine")
.setContentText("Download completed");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(455, mBuilder.build());
}
};