安卓:在安装应用程序时检测应用程序

2023-09-05 09:26:51 作者:回不去的此间年少

我想从服务器下载使用下载管理器类Android应用程序,安装它,然后检测时,安装完成。我使用两个接收器:一个用于检测下载过程,并在其它用于检测安装过程。第一接收机工作正常,但第二个没有。我做错了什么?

 下载管理器DM =(下载管理器)DownloadApplicationActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request REQ =新DownloadManager.Request(Uri.parse(MY_LINK));
req.setTitle(MY_TITLE)
                .setDescription(下载......)
                //下载包到/ SD卡/立即下载路径。
                .setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS,
                        my_path的);
        长时间排队= dm.enqueue(REQ);
BroadcastReceiver的接收器=新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    串动= intent.getAction();
    如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
        查询查询=新的Query();
        query.setFilterById(排队);
        光标C = dm.query(查询);
        如果(c.moveToFirst()){
            INT参数:columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
            如果(DownloadManager.STATUS_SUCCESSFUL == c.getInt(参数:columnIndex)){
                //显示一个通知栏。
                NotificationManager notificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
                通知通知=新的通知(R.drawable.icon,,System.currentTimeMillis的());

                notification.flags | = Notification.FLAG_AUTO_CANCEL;
                notification.flags | = Notification.FLAG_NO_CLEAR;
                意图I =新的意图(Intent.ACTION_VIEW);
                //通知被点击时,安装应用程序。
                        i.setDataAndType(Uri.fromFile(新文件(环境
                                .getExternalStorageDirectory()+ APP_PATH)),应用程序/ vnd.android.package存档);
                        PendingIntent pendingIntent = PendingIntent.getActivity(
                                活性,0,I,0);
                        notification.setLatestEventInfo(活动,MY_TEXT,MY_TEXT,pendingIntent);
                        notification.number + = 1;
                        notificationManager.notify(0,通知);
                        //我要检测的应用程序的安装,我注册一个东北接收器
                        registerReceiver(installReceiver,新的IntentFilter(Intent.ACTION_PACKAGE_ADDED));
            }
        }
};

BroadcastReceiver的installReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    串动= intent.getAction();
    如果(Intent.ACTION_PACKAGE_ADDED.equals(动作)){
        乌里数据= intent.getData();
        字符串的packageName = data.getEn codedSchemeSpecificPart();
        Log.i(已安装的软件包是:,+的packageName);

            }
        }
};
 

解决方案

我解决我的问题,我补充

 的IntentFilter的IntentFilter =新的IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme(包);
 

前行:

  registerReceiver(installReceiver,IntentFilter的);
 
安卓手机如何安装应用程序

I am trying to download an Android app from a server using the DownloadManager class, install it and then detect when the installation is completed. I am using two receivers: one to detect the download process and the other to detect the install process. The first receiver works properly, but the second doesn't. What I am doing wrong?

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

            }
        }           
};

解决方案

I solved my problem, I added

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

before the line :

registerReceiver(installReceiver, intentFilter);