Android - Conseguir el título y la descripción de BroadcastReceiver
-
22-12-2019 - |
Pregunta
Estoy trabajando en una aplicación android.
Estoy usando BroadcastReciever para obtener el sabe cuando una descarga ha terminado.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlString));
request.setDescription(artistString);
request.setTitle(titleString);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC + "/Tunesto", titleString + " - " + artistString + ".mp3");
Toast.makeText(mainContext, "Downloading " + titleString + " - " + artistString, Toast.LENGTH_SHORT).show();
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Entonces puedo usar esto en mi método onCreate para mostrar un brindis cuando la descarga ha terminado:
onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String info = "";
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_DESCRIPTION);
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TITLE);
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_ID);
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_LOCAL_FILENAME);
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_MEDIA_TYPE);
info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
info = info + " - " + intent.getDataString();
info = info + " - " + intent.getType();
info = info + " - " + intent.EXTRA_TITLE;
info = info + " - " + intent.EXTRA_TEXT;
//Toast.makeText(mainContext, Integer.valueOf(dlCount) + " Download \"" + titleString + " - " + artistString + "\" completed", Toast.LENGTH_LONG).show();
Toast.makeText(mainContext, info + "Download completed", Toast.LENGTH_LONG).show();
}
};
registerReceiver(onComplete, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Como usted puede ver, he probado de todo para obtener la descripción y el título dentro de la información de la cadena, pero no muestran nada más que nulo (el brindis muestra algo como ...
Puede usted por favor me dan un consejos sobre cómo recuperar esas informaciones?
Solución
Puede usted por favor me dan un consejos sobre cómo recuperar esas informaciones?
Leer en el valor para EXTRA_DOWNLOAD_ID
, llamando getLongExtra()
en la emisión Intent
.A continuación, utilice que con DownloadManager.Query
para recuperar los datos para esta descarga.El Cursor
volver a llamar query()
en DownloadManager
contendrá los valores codificados por el COLUMN_
las constantes.
Este blog tiene el código de ejemplo que ilustra este proceso.