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

Posted

技术标签:

【中文标题】Android:检测何时安装应用程序【英文标题】:Android: detect when app is installed 【发布时间】:2012-07-08 19:08:47 【问题描述】:

我正在尝试使用 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);

            
                   
;

【问题讨论】:

参考之前的帖子***.com/questions/11392183/… 您发布的帖子检查应用程序是否已安装。我想要的是在安装完成后检测安装。 【参考方案1】:

我解决了我的问题,我添加了

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

行前:

registerReceiver(installReceiver, intentFilter);

【讨论】:

.addDataScheme() 行是我所缺少的! ACTION_PACKAGE_REMOVED 需要类似的东西吗?所有这些都记录在哪里? 相反,您也可以将它们添加到您的清单文件中,以便您的代码更容易跟踪。【参考方案2】:

你可以试试下面的代码。通过这种方式,您可以获得所有可以由意图调用的活动,并且如果您知道活动名称并且它存在于queryIntentActivities()..检索到的列表中,那么您就知道它已安装。

public void callQrScan()
    
    Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");    

    if(isCallable(intent1)== true)



    Context context = getApplicationContext();
    CharSequence text = "Scan Niet Gelukt";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    
    else
        Context context = getApplicationContext();
        CharSequence text = "Scan Niet Gelukt";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    


    Button tempbutton = (Button)findViewById(R.id.Button03);

    tempbutton.setOnClickListener(new OnClickListener()
    
        public void onClick(final View v)
    
                callQrScan();
    
    );

    

    private boolean isCallable(Intent intent1)   
                List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent1,   
            PackageManager.MATCH_DEFAULT_ONLY);  
        if(list.size() > 0)
                return true ;  
        else
            return false;

    

希望对你有帮助:)

【讨论】:

这没有帮助。这不是我要找的。​​span>

以上是关于Android:检测何时安装应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Android webview如何检测何时重新加载

Android:检测 GPS 何时打开/关闭(或当没有应用程序不再使用它时)

Android:检测 GPS 何时打开/关闭(或当没有应用程序不再使用它时)

Android:如何通过通知下拉菜单检测设置何时更改?

是否可以在 JavaScript 中检测 Android 和 iOS 浏览器何时关闭屏幕

如何检测用户何时在 android 设备上插入耳机? (与 ACTION_AUDIO_BECOMING_NOISY 相反)