简体   繁体   English

以编程方式安装apk而不定义apk名称

[英]installing apk programmatically without defining the apk name

I am trying to install apk programmatically from SD card without mentioning the name of the apk. 我试图从SD卡编程安装apk而不提及apk的名称。 What I can do now is I can get installed the apk I named in my code. 我现在可以做的是我可以安装我在我的代码中命名的apk。 But it is not handy if I want to have another apk installed on my device and for that I have to go into my code and change the name of the apk. 但是如果我想在我的设备上安装另一个apk并且为此我必须进入我的代码并更改apk的名称,这不方便。 Is there any way to get the name of the apk file dynamically on run time? 有没有办法在运行时动态获取apk文件的名称? For instance getting a list of installable apk, so that user can choose which one to install? 例如获取可安装的apk列表,以便用户可以选择安装哪一个?

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file:///mnt/sdcard/extsd/download   /app.apk"),
"application/vnd.android.package-archive");
startActivity(intent);

Update: The previous code was deleted because contain errors. 更新:之前的代码已删除,因为包含错误。 Here is a working code: 这是一个有效的代码:

public class InstallAPKActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExtFilter apkFilter = new ExtFilter("apk");
        File file[] = Environment.getExternalStorageDirectory().listFiles(apkFilter);
        Log.d("InstallApk", "Filter applied. Size: "+ file.length);

        for (int i=0; i < file.length; i++)
        {
            Log.d("InstallApk", "FileName:" + file[i].getName());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file[i]), "application/vnd.android.package-archive");
            startActivity(intent);
        }





    }

    class ExtFilter implements FilenameFilter { 
        String ext; 
        public ExtFilter(String ext) { 
            this.ext = "." + ext; 
        } 
        public boolean accept(File dir, String name) { 
            return name.endsWith(ext); 
        }
    }
}

Update 2: This program simply enumerates all apk file and writes them to the array of File. 更新2:此程序只是枚举所有apk文件并将它们写入File数组。 After that it tries to install all this apk files sequentially. 之后,它尝试按顺序安装所有这些apk文件。 For instance, in my case I put application golddream.apk on a sdcard of my emulator. 例如,在我的情况下,我将应用程序golddream.apk放在我的模拟器的SD卡上。 The application is developed for SDK v 10. I see the following output in my logcat: 该应用程序是为SDK v 10开发的。我在logcat中看到以下输出:

12-21 06:44:39.453: D/InstallApk(14897): Filter applied. Size: 1
12-21 06:44:39.453: D/InstallApk(14897): FileName:golddream.apk
12-21 06:44:39.463: I/ActivityManager(62): Starting: Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/golddream.apk typ=application/vnd.android.package-archive cmp=com.android.packageinstaller/.PackageInstallerActivity } from pid 14897
12-21 06:44:40.073: I/ActivityManager(62): Displayed com.android.packageinstaller/.PackageInstallerActivity: +578ms (total +1s229ms)

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

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