Java 下载接收器——Android

Java 下载接收器——Android,java,android,android-activity,Java,Android,Android Activity,我正在研究如何在任何应用程序中发生任何下载时触发我的应用程序。我有一个使用DownloadManager的代码片段,它只会在我的应用程序执行任何下载时通知我,但尽管我从未找到任何在我的手机上进行任何下载的解决方案,但它必须通知我的应用程序。请告诉我这是否可行。谢谢1-您必须在后台下载任何文件 public class DownloadService extends Service { private static final String TAG = "DownloadServ

我正在研究如何在任何应用程序中发生任何下载时触发我的应用程序。我有一个使用DownloadManager的代码片段,它只会在我的应用程序执行任何下载时通知我,但尽管我从未找到任何在我的手机上进行任何下载的解决方案,但它必须通知我的应用程序。请告诉我这是否可行。谢谢

1-您必须在后台下载任何文件

    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-然后您必须在下载完成时发出通知

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>

------
------
------
------

你可以使用广播接收器。你能给我简要解释一下吗。如果您有任何参考资料,请谅解。这可能会有所帮助,但您必须更改您试图跟踪的目的的名称