I would like to receive an event whenever a file was added to a specific folder, e.g. the download folder. To reach this I tried 3 different approaches without any success. The
I have tried your APPROACH 3-BroadcastReceiver in my android Application and it works for me. Please go through the code it may help you.You can change the image url according to your requirements.
My Activity and Broadcast receiver class:
package com.example.sudiproy.downloadingpath;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private DownloadManager downloadManager;
private Button startDownload;
private Button checkStatusOfDownload;
private long downloadReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDownload = (Button) findViewById(R.id.btn_download);
checkStatusOfDownload = (Button) findViewById(R.id.btn_check_status);
startDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("http://demo.mysamplecode.com/Sencha_Touch/CountryServlet?start=0&limit=999");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Set title to be displayed if notification is enabled
request.setTitle("My Data Download");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(MainActivity.this, Environment.DIRECTORY_DOWNLOADS, "CountryList.json");
//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);
}
});
checkStatusOfDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterById(downloadReference);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(myDownloadQuery);
if (cursor.moveToFirst()) {
checkStatus(cursor);
}
}
});
}
private void checkStatus(Cursor cursor) {
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
//column for reason code if the download failed or paused
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
//get the download filename
int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String filename = cursor.getString(filenameIndex);
String statusText = "";
String reasonText = "";
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS FAILED";
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
reasonText = "Filename:\n" + filename;
break;
}
}
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadReference == referenceId) {
int ch;
ParcelFileDescriptor file;
StringBuffer strContent = new StringBuffer("");
StringBuffer countryData = new StringBuffer("");
try {
file = downloadManager.openDownloadedFile(downloadReference);
FileInputStream fileInputStream
= new ParcelFileDescriptor.AutoCloseInputStream(file);
while ((ch = fileInputStream.read()) != -1)
strContent.append((char) ch);
JSONObject responseObj = new JSONObject(strContent.toString());
JSONArray countriesObj = responseObj.getJSONArray("countries");
for (int i = 0; i < countriesObj.length(); i++) {
Gson gson = new Gson();
String countryInfo = countriesObj.getJSONObject(i).toString();
Country country = gson.fromJson(countryInfo, Country.class);
countryData.append(country.getCode() + ": " + country.getName() + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}
My Pojo class:
package com.example.sudiproy.downloadingpath;
public class Country {
String code = null;
String name = null;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My Manifest Declaration: