在android中安装apk

时间:2014-09-23 05:42:51

标签: android android-intent install apk

美好的一天!我正在尝试安装下载的apk.I have upload to     http://w194442.open.ge.tt/1/files/........?download 我通过以下代码下载它:

   downloadUrl = urlStringChooser(myURL);
   String downloadCompleteIntentName = DownloadManager.ACTION_DOWNLOAD_COMPLETE;
   IntentFilter downloadCompleteIntentFilter = new   
   IntentFilter(downloadCompleteIntentName);
   context = getApplicationContext();
   context.registerReceiver(downloadCompleteReceiver, downloadCompleteIntentFilter);


   String servicestring = Context.DOWNLOAD_SERVICE;

   downloadmanager = (DownloadManager) getSystemService(servicestring);
   Uri uri = Uri.parse(downloadUrl);
   DownloadManager.Request request = new DownloadManager.Request(uri);
   downloadID = downloadmanager.enqueue(request);

之后我从系统下载了一个广播信号:

 private BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
        if (id != downloadID) {
            private BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
        if (id != downloadID) {
            Toast toast = Toast.makeText(getApplicationContext(), "downloadID FAIL", Toast.LENGTH_LONG);
            toast.show();
            return;
        }
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(id);
        Cursor cursor = downloadManager.query(query);
        if (!cursor.moveToFirst()) {
            Toast toast = Toast.makeText(getApplicationContext(), "cursor.moveToFirst FAIL", Toast.LENGTH_LONG);
            toast.show();
            return;
        }

        int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
            Toast toast = Toast.makeText(getApplicationContext(), "cDownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)", Toast.LENGTH_LONG);
            toast.show();
            return;
        }

        int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String downloadedPackageUriString = cursor.getString(uriIndex);

        uri = Uri.parse(downloadedPackageUriString);
        if(uri != null) {
            Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG);
            toast.show();

            installUpdate(uri);
        }
    }
};
            return;
        }
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(id);
        Cursor cursor = downloadManager.query(query);
        if (!cursor.moveToFirst()) {
            Toast toast = Toast.makeText(getApplicationContext(), "cursor.moveToFirst   
               FAIL", Toast.LENGTH_LONG);
            toast.show();
            return;
        }

        int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
            Toast toast = Toast.makeText(getApplicationContext(), 
            "cDownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)",  
            Toast.LENGTH_LONG);
            toast.show();
            return;
        }

        int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String downloadedPackageUriString = cursor.getString(uriIndex);

        uri = Uri.parse(downloadedPackageUriString);
        if(uri != null) {
            Toast toast = Toast.makeText(getApplicationContext(), "Success", 
            Toast.LENGTH_LONG);
            toast.show();

            installUpdate(uri);
           }
        }
    };

现在工作正常。然后我调用installUpdate(uri);安装它。

     Intent promptInstall = new Intent(Intent.ACTION_VIEW);
     promptInstall.setClass(context, MyActivity.class);
     promptInstall.setDataAndType(goodUri,"application/vnd.android.package-archive");
     promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     Log.d(TAG, "Before  startActivity ");
     startActivity(promptInstall);

没有任何反应。 Logcat是

09-23 05:30:07.531    9492-9492/regulatory.securitycode.com.vpnupdate D/Updater﹕ Before   
startActivity

09-23 05:30:07.556    9492-9492/regulatory.securitycode.com.vpnupdate E/dalvikvm﹕    
dvmPauseGc(AppLaunch) called - cookie=0xf692 (f=0x1)

09-23 05:30:07.566    9492-9492/regulatory.securitycode.com.vpnupdate   
E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView

09-23 05:30:07.611    9492-9492/regulatory.securitycode.com.vpnupdate D/mali_winsys﹕ 
new_window_surface returns 0x3000

09-23 05:30:07.651    9492-9492/regulatory.securitycode.com.vpnupdate E/dalvikvm﹕  
dvmResumeGc(0xf692, 0) called (f=0x1)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否在安全设置中选中了“从未知来源安装”选项?