接收器下载 - Android

时间:2015-11-05 09:31:52

标签: java android android-activity

我正在研究如何在任何应用程序中发生任何下载时触发​​我的应用程序。我有使用DownloadManager的代码片段,只有当我的应用程序执行了任何下载时才会通知我,但是我从来没有找到任何解决方案,在我的手机中发生任何下载,必须通知我的应用程序。 Pl建议我是否可以这样做。谢谢

1 个答案:

答案 0 :(得分:0)

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);
  • Manifest.xml中的更改

在应用程序标记内的清单文件中添加标记:

<application >
------
------

<service android:name="PACKAGENAME.DownloadService" />

------
------
</application>