安装APK programmattically没有确定的apk名APK、programmattically、apk

2023-09-04 05:28:05 作者:劳资不是限量版是特么绝版

我想不提的apk的名字从SD卡安装APK编程。我现在能做的就是我可以安装在我的code命名的apk。但它不是方便,如果我想有另一个APK安装我的设备上,并为我进入我的code和改变的apk的名字。有没有办法让在运行时动态的apk文件的名称?比如让安装apk文件的列表,让用户可以选择安装哪一个?

I am trying to install apk programmatically from SD card without mentioning the name of the apk. What I can do now is I can get installed the apk I named in my code. 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. Is there any way to get the name of the apk file dynamically on run time? For instance getting a list of installable apk, so that user can choose which one to install?

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

感谢你在前进!圣诞快乐!!!所有的开发者和观众。

Thank you in advance"!!! Merry Christmas!!! to all the developers and viewers.

推荐答案

更​​新:previous code被删除,因为包含错误。这是一个工作code:

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

更新2: 这个程序只是枚举所有APK文件,并将其写入文件的数组。之后,它会尝试安装所有这个apk文件顺序。举例来说,在我的情况,我把应用程序golddream.apk我模拟器的SD卡。该应用程序的SDK v 10.我看到下面的输出在我的logcat的开发:

Update 2: This program simply enumerates all apk file and writes them to the array of File. After that it tries to install all this apk files sequentially. For instance, in my case I put application golddream.apk on a sdcard of my emulator. The application is developed for SDK v 10. I see the following output in my 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)