0
我正在研究哦如何觸發我的應用程序,如果任何下載發生在任何應用程序。我有使用DownloadManager的代碼片段,只有當我的應用程序執行任何下載時,纔會通知我,但我從來沒有找到任何解決方案,在我的移動設備上發生任何下載,必須通知我的應用程序。 Pl建議我,如果這將是可能的。謝謝接收器下載 - Android
我正在研究哦如何觸發我的應用程序,如果任何下載發生在任何應用程序。我有使用DownloadManager的代碼片段,只有當我的應用程序執行任何下載時,纔會通知我,但我從來沒有找到任何解決方案,在我的移動設備上發生任何下載,必須通知我的應用程序。 Pl建議我,如果這將是可能的。謝謝接收器下載 - Android
1-您必須製作Service才能下載後臺中的任何文件。
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 - 那麼你必須做出ResultReceiver得到通知,而下載完成。
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-呼叫下載服務
Intent intent = new Intent(context, DownloadService.class);
intent.putExtra("url", "url to download");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);
添加標籤:
<application >
------
------
<service android:name="PACKAGENAME.DownloadService" />
------
------
</application>
您可以使用BroadCastReceiver的。 –
你可以給我簡要的解釋。欣賞,如果你有任何參考。 – RAAAAM
這可能有助於http://stackoverflow.com/questions/22950893/how-to-detect-if-a-specific-app-is-being-downloaded-installed-by-the-google-play –