简体   繁体   English

在Android中安装APK

[英]Installing apk in android

Good day! 美好的一天! I'm trying to install downloaded apk.I've uploaded it to http://w194442.open.ge.tt/1/files/........?download I download it by this code: 我正在尝试安装下载的apk,我已将其上传到http://w194442.open.ge.tt/1/files / ........ 下载通过以下代码下载:

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

After that I get a broadcast signal from system that file downloaded : 之后,我从系统收到一个广播信号,说明该文件已下载:

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

Works fine for now. 现在工作正常。 Then I call installUpdate(uri); 然后我打电话给installUpdate(uri); to install it. 安装它。

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

And nothing happens. 没有任何反应。 Logcat is 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)

What do I do wrong? 我做错了什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM