接收器下载 - 安卓 [英] Receiver for download -- Android

查看:252
本文介绍了接收器下载 - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做研究哦如何触发我的应用程序,如果任何的下载发生在任何应用程序中。我有code段,它使用下载管理器,并且会通知我,只有当我的应用程序必须执行任何下载,但而我从来没有找到的任何下载发生在我的移动,那是否要通知我的应用程序的任何解决方案。 PL建议我,如果这将是可能的。谢谢

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

推荐答案

1 - 你必须做的服务以下载背景中的任何文件。

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 - 那么你必须做出 ResultReceiver 得到通知,而下载完毕。

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呼叫下载服务

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);

  • 在变化的Manifest.xml
  • 添加标签的清单文件中Application标签:

    Add tag in manifest file inside Application tag:

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

    这篇关于接收器下载 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆