Android:检测安装应用程序的时间

Android:检测安装应用程序的时间,第1张

概述我正在尝试使用DownloadManager类从服务器下载Android应用程序,安装它然后检测安装何时完成.我使用两个接收器:一个用于检测下载过程,另一个用于检测安装过程.第一个接收器工作正常,但第二个接收器没有.我做错了什么?DownloadManagerdm=(DownloadManager)DownloadApplicationAc

我正在尝试使用DownloadManager类从服务器下载Android应用程序,安装它然后检测安装何时完成.我使用两个接收器:一个用于检测下载过程,另一个用于检测安装过程.第一个接收器工作正常,但第二个接收器没有.我做错了什么?

DownloadManager dm = (DownloadManager) DownloadApplicationActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);DownloadManager.Request req = new DownloadManager.Request(Uri.parse(MY_link));req.setTitle(MY_Title)                .setDescription("Downloading ....")                // download the package to the /sdcard/downlaod path.                .setDestinationInExternalPublicDir(                        Environment.DIRECTORY_DOWNLOADS,                        MY_PATH);        long enqueue = dm.enqueue(req);broadcastReceiver receiver= new broadcastReceiver() {@OverrIDepublic voID onReceive(Context context, Intent intent) {    String action = intent.getAction();    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {        query query = new query();        query.setFilterByID(enqueue);        Cursor c =dm.query(query);        if (c.movetoFirst()) {            int columnIndex = c.getColumnIndex(DownloadManager.ColUMN_STATUS);            if (DownloadManager.STATUS_SUCCESSFul == c.getInt(columnIndex)) {                // show a notification bar.                notificationmanager notificationmanager = (notificationmanager) this.getSystemService(Context.NOTIFICATION_SERVICE);                Notification notification = new Notification(R.drawable.icon,"",System.currentTimeMillis());                notification.flags |= Notification.FLAG_auto_CANCEL;                notification.flags |= Notification.FLAG_NO_CLEAR;                Intent i = new Intent(Intent.ACTION_VIEW);                // when the notification is clicked, install the app.                        i.setDataAndType(Uri.fromfile(new file(Environment                                .getExternalStorageDirectory() + APP_PATH)),"application/vnd.androID.package-archive");                        PendingIntent pendingIntent = PendingIntent.getActivity(                                activity, 0, i, 0);                        notification.setLatestEventInfo(activity, MY_TEXT, MY_TEXT,pendingIntent);                        notification.number += 1;                        notificationmanager.notify( 0, notification);                        //i want to detect the app's installation, I register a ne receiver                        registerReceiver(installreceiver,new IntentFilter(Intent.ACTION_PACKAGE_ADDED));            }        }           };  broadcastReceiver installreceiver= new broadcastReceiver() {@OverrIDepublic voID onReceive(Context context, Intent intent) {    String action = intent.getAction();    if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {        Uri data = intent.getData();        String packagename = data.getEncodedSchemeSpecificPart();        Log.i("The installed package is: ", "" + packagename);            }        }           };

解决方法:

我补充说,我解决了我的问题

IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);intentFilter.addDataScheme("package");

在行之前:

registerReceiver(installreceiver, intentFilter);
总结

以上是内存溢出为你收集整理的Android:检测安装应用程序的时间全部内容,希望文章能够帮你解决Android:检测安装应用程序的时间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存