Android:检测何时安装了应用程序

标签 android broadcastreceiver android-download-manager

我正在尝试使用 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() {
@Override
public 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() {
@Override
public 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:检测何时安装了应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394141/

相关文章:

android - JSONArray 与 JSONObject

从 SD 卡获取 Android 歌曲

安卓房间 : how to query related entities without foreign key column

android - 为什么在使用 DownloadManager 时会出现 IllegalArgumentException?

Android DownloadManager remove() - 如何确定 remove() 操作何时完成?

android - Kotlin Gradle 问题

java - 如何获取android中类中的当前上下文?

android - BroadcastReceiver 是否可以从 Android 操作系统中终止以释放内存?

android - Android 可以在没有 Activity#onResume 的情况下调用 Activity#onPause,还是 Context#registerReceiver 会失败?

Android:下载管理器和 COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED