如何从url安装apk并以编程方式安装

时间:2014-03-17 08:17:53

标签: android

即时通讯在我的应用程序中创建一个功能,从服务器上下载新版本的apk并安装但是即时面临问题经过这么多次切换我发现这个代码如下 当我在这行c.connect();之后进行调试时,catch()并打印Toast消息有什么问题请帮我,我的网址正在工作你可以尝试在webbroser上有人给我这个在线apk网址

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Button  button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


   Update("https://dl.dropboxusercontent.com/u/245131571/SampleApp.apk");       



        }

    });



}





      public void Update(String apkurl){
          try {
                URL url = new URL(apkurl);
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");

                c.connect();

 String PATH = Environment.getExternalStorageDirectory() + "/naz/";
                File file = new File(PATH);
                file.mkdirs();
                File outputFile = new File(file, "app.apk");
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();//till here, it works fine - .apk is download to my sdcard in download file

                Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse(PATH+"app.apk"))
                .setType("application/android.com.app");
                startActivity(promptInstall);//installation is not working

            } catch (IOException e) {
 Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
            }
         }  



}

AndroidManifest

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
 <uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.appinstall.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

 </manifest>

2 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

答案 1 :(得分:0)

请使用上述代码在下面找到完整的工作示例。 这个答案只是为了使用上述权限(

)收集完整的工作示例
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>

在这里输入代码

活动如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Update("https://dl.dropboxusercontent.com/u/245131571/SampleApp.apk");

            }

        });

    }

    public void Update(final String apkurl) {
        new AsyncTask<Void, String, String>() {
            String result="";
            @Override
            protected String doInBackground(Void... params) {
                try {
                    URL url = new URL(apkurl);
                    HttpURLConnection c = (HttpURLConnection) url
                            .openConnection();
                    c.setRequestMethod("GET");

                    c.connect();

                    String PATH = Environment.getExternalStorageDirectory()
                            + "/download/";
                    File file = new File(PATH);
                    file.mkdirs();
                    File outputFile = new File(file, "app.apk");
                    FileOutputStream fos = new FileOutputStream(outputFile);

                    InputStream is = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                    }
                    fos.close();
                    is.close();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                } catch (IOException e) {
                    result="Update error! "+ e.getMessage();
                    e.printStackTrace();

                }
                return result;
            }

            protected void onPostExecute(String result) {

                Toast.makeText(getApplicationContext(), result,
                        Toast.LENGTH_LONG).show();
            };

        }.execute();

    }

}