putExtra overriding other putExtra
In the for-loop below the last putExtra overrides the first putExtra even though they take different parameters and different vars.
First: putExtra(String,String) Second: putExtra(String, int)
Note: videos[i] is a String array
Intent intent = new Intent(this,DownloadService.class);
for(int i=0;i<videos.length;i++){
intent.putExtra("VIDEOS",videos[i]);
intent.putExtra("FILENR",(i + 1));
startService(intent);
}
In my DownloadService which extends IntentService the value from VIDEOS is null !! Why is that ?
EDIT: This is the part of the code in my DownloadService class:
Intent broadcastIntent = new Intent();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, null, 0);
notification = new Notification(R.drawable.icon, "Skitips", System.currentTimeMillis());
contentView = new RemoteViews(getPackageName(), R.layout.progress_layout);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.contentView = contentView;
notification.contentIntent = contentIntent;
contentView.setProgressBar(R.id.status_progress, 100, 0, false);
contentView.setTextViewText(R.id.status_text,"Downloading...");
String full_url = "";
full_url = URL + intent.getStringExtra("VIDEOS");
String fileName = "";
fileName = intent.getStringExtra("VIDEOS");
String fileNr = "";
fileNr = intent.getStringExtra("FILENR");
int count;
Log.e("Whattttttt","is the value of VIDEOOOOSSSS: " + full_url);
Log.e("Whatttt","is the value of fileNrrrrrrrrrrrr:" + fileNr);
try {
URL url = new URL(full_url);
URLConnection conexion = url.openConnection();
conexio开发者_JS百科n.connect();
File file = new File(root.getPath(), fileName);
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
contentView.setTextViewText(R.id.status_text,"Downloading (" + fileNr + "/77)");
in the bold text there fileNr i need to get the fileNr from the other activity but it's giving me null in this case...
that's because putExtra() add data to an hashmap. With hashmap if you use the same key the previous value is overridden by the most recent.
In your Question the
key are always remain same for different values that's why,,
try to use
intent.putExtra(DownloadService.VIDEOS+i,videos[i]);
intent.putExtra(DownloadService.fileNr+i, i++);
and get result like,
DownloadService.VIDEOS0,DownloadService.fileNr0,
DownloadService.VIDEOS1,DownloadService.fileNr1,
EDIT: try this code..
Intent intent = new Intent(this,DownloadService.class);
putStringArrayListExtra("Videos_key", videos);
startService(intent);
And one more thing if you are passing array of integer
and some Array of video URI
then please try to use
putStringArrayListExtra(String name, ArrayList<String> value)
putIntegerArrayListExtra(String name, ArrayList<Integer> value)
and avoid loop..
Thanks.
Well of course they overwrite each other, since you write them all with the same key (DownloadService.VIDEOS
and DownloadService.fileNr
are the same for all values).
You may add extra key like
intent.putExtra("number", videos.length);
outside your loop and then write values as
intent.putExtra(DownloadService.VIDEOS + i,videos[i]);
intent.putExtra(DownloadService.fileNr + i, i++);
And to read them read number
first and use loop to read your values
You are not giving different key name in your loop.Since you loop will be run more than once then next time you are puting the value videos[i] in previous key only in DownloadService.VIDEOS so try to give unique name for your key DownloadService.VIDEOS for second value and so on.Similary give unique name for DownloadService.fileNr when loop run more than once
Correct answer will be add i as a string in your key and then you get it by giving key name
putting the string value
intent.putExtra(DownloadService.VIDEOS + i,videos[i]);
intent.putExtra(DownloadService.fileNr +i, i++);
getting the string value
for(int i=0;i<videos.length;i++)
{
intent.getExtra(DownloadService.VIDEOS + i);
}
精彩评论