4

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

            }
        }           
};
4

2 回答 2

8

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

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

行前:

registerReceiver(installReceiver, intentFilter);
于 2012-07-09T12:39:29.530 回答
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;

    }

希望能帮助到你 :)

于 2012-07-09T11:56:25.377 回答