i am doing research oh how to trigger my application if any of download happens in any of application. I have code snippet which uses DownloadManager and that will notify me only when my application had perform any of download, but whereas i never find any solution on which any of download happens in my mobile, that has to notify my application. Pl suggest me if this will be possible. Thanks
Asked
Active
Viewed 557 times
0
-
You can use BroadCastReceiver for that. – Chirag Savsani Nov 05 '15 at 09:36
-
Can you pl give me brief explanation. Appreciate, if you have any references . – RAAAAM Nov 05 '15 at 09:38
-
this may help http://stackoverflow.com/questions/22950893/how-to-detect-if-a-specific-app-is-being-downloaded-installed-by-the-google-play – Nouran S. Ahmad Nov 05 '15 at 09:49
-
but you would have to change the name of the intent your trying to track – Nouran S. Ahmad Nov 05 '15 at 09:50
1 Answers
0
1- You have to make Service for Download any file in Background.
public class DownloadService extends Service {
private static final String TAG = "DownloadService";
public static final int UPDATE_PROGRESS = 8344;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
} else {
final String urlToDownload = intent.getStringExtra("url");
final ResultReceiver receiver = (ResultReceiver) intent
.getParcelableExtra("receiver");
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url
.openStream());
String localPath = Environment
.getExternalStorageDirectory()
.getAbsoluteFile()
+ File.separator
+ Constant.ROOT_FOLDER_NAME
+ File.separator
+ Constant.FOLDER_IMAGE
+ File.separator
+ urlToDownload.substring(urlToDownload
.lastIndexOf('/') + 1);
AppLog.Log(TAG, "Path :: " + localPath);
OutputStream output = new FileOutputStream(localPath);
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));
receiver.send(UPDATE_PROGRESS, resultData);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
AppLog.Log(TAG, "********* EXCEPTION *****");
e.printStackTrace();
}
Bundle resultData = new Bundle();
resultData.putInt("progress", 100);
receiver.send(UPDATE_PROGRESS, resultData);
stopSelf();
}
}).start();
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
2- Then you have to Make ResultReceiver to get notifying while download is completed.
private class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
if (progress == 100) {
// Download Complete
}
}
}
}
3- Call Download Service
Intent intent = new Intent(context, DownloadService.class);
intent.putExtra("url", "url to download");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);
- Changes in Manifest.xml
Add tag in manifest file inside Application tag:
<application >
------
------
<service android:name="PACKAGENAME.DownloadService" />
------
------
</application>

Sagar Zala
- 4,854
- 9
- 34
- 62