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

时间:2012-07-09 11:27:41

标签: 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);

            }
        }           
};

2 个答案:

答案 0 :(得分:6)

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

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

行之前:

registerReceiver(installReceiver, intentFilter);

答案 1 :(得分:0)

您可以尝试以下代码。通过这种方式,您可以获取意图可以调用的所有活动,如果您知道活动名称,并且它存在于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;

    }

希望有所帮助:)