如何下载&在Android应用程序中安装.apk文件?

时间:2012-04-18 06:20:57

标签: android

我有一个java Web应用程序通过这个应用程序我发送URL下载.apk文件。现在我必须在我的Android模拟器浏览器中使用此URL下载.apk文件并自动安装。 那么有人可以给我这个问题的解决方案吗?

3 个答案:

答案 0 :(得分:3)

只需执行以下步骤:

1,下载文件使用Asynctask或Service。解决方案here

2,将下载的文件传递给软件包安装程序。

Intent intent = new Intent(Intent.ACTION_VIEW);

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

startActivity(intent);

这是下载文件和mime类型的简单设置路径。更多细节 here 。你也可以向右看。有一个像你这样的相关问题列表

答案 1 :(得分:0)

在浏览器类型网址中按回车键,会要求安装。但文件自动安装过程基于Device。如果您选择Htc mobile它将自动安装但不适用于三星手机

答案 2 :(得分:0)

不要忘记运行时权限

这个简单的示例适用于API 28。 它会打开一个apk文件,可从“下载文件夹”中安装

为简单起见: 将要安装的应用程序的apk文件下载到手机的“下载”文件夹中。 (有很多说明可以通过编程方式进行,也可以手动进行)

要做

  • 创建新项目
  • 向MainActivity添加按钮
  • 在res文件夹中创建xml文件夹,并在其中创建file_paths.xml文件
  • 使用下面的代码
  • 享受=)

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.teko.testcleanopenfile">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
</manifest>

MainActivity


public class MainActivity extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // textView and button
        textView = findViewById(R.id.textView);textView.setText("Hello updatable World\n");
        (findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {RunAPK(getBaseContext());}
        });
    }

    private void RunAPK(Context context){
        requestPermissionsToRead();
    }

    private void requestPermissionsToRead() {
        // ASK RUNTIME PERMISSIONS
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE},111);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0) {
            if (requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                textView.append("Permission granted write\n");

                // Create Uri
                File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File file1 = new File (downloads + "//app-debug.apk");//downloads.listFiles()[0];
                Uri contentUri1 = getUriForFile(this, BuildConfig.APPLICATION_ID, file1);

                // Intent to open apk
                Intent intent = new Intent(Intent.ACTION_VIEW, contentUri1);
                intent.setDataAndType(contentUri1, "application/vnd.android.package-archive");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
            }
        }
    }
}

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="download" path="."/>
</paths>
相关问题