简体   繁体   English

从市场上安装APK,并从应用程序内快速安装

[英]Installing APK from market with prompt install from within application

I have a question that I see a few answers for on stack overflow, but to no avail. 我有一个问题,我在堆栈溢出时看到了一些答案,但无济于事。 What im trying to do is this: 我试图做的是这样的:

I am making an application that checks if an app is installed first. 我正在制作一个应用程序,用于检查是否首先安装了应用程序。 For example, com.appA.android. 例如com.appA.android。

If appA is not installed, it prompts the user with prompt install to install the app before continuing. 如果未安装appA,它会提示用户安装提示,然后继续安装。 Im not trying to install APKs without user permission, but with their permission. 我不是在没有用户许可的情况下尝试安装APK,而是在获得他们许可的情况下安装。 appA is in the store, but i dont want my users to be pulled away from the application. appA在商店中,但是我不希望我的用户离开该应用程序。 Currently, my way of doing this looks like: 目前,我的操作方式如下:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                      .setDataAndType(Uri.parse("file:///path/to/apk"),
               "application/vnd.android.package-archive");
               startActivity(promptInstall);

any way i can do this with a market link? 我可以通过市场链接做到这一点吗?

Thanks for any help! 谢谢你的帮助!

For security Reasons you can't install APK without an installation Dialog or within the Background. 出于安全原因,没有安装对话框或在后台无法安装APK。 You can install APK's in the background with root and the Packagemanager. 您可以使用root和Packagemanager在后台安装APK。

There are several attempts to listen for the installation of the app, so you can restart you activity. 有几种尝试监听应用程序安装的尝试,因此您可以重新启动活动。 One is registering a BroadcastReceiver, which listen for an Installation of the App. 一个正在注册一个BroadcastReceiver,它监听该应用程序的安装。

<receiver android:name=".PackageReceiver">
<intent-filter>
    <action android:name="android.intent.action.PACKAGE_ADDED" />
    <action android:name="android.intent.action.PACKAGE_CHANGED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="package" />
</intent-filter>

This class then gets called when a new package is installed: 然后在安装新软件包时调用此类:

 public class PackageReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 // handle install event here
 }
}

A second attempt is having a Timer which try to find the app all nSeconds for installation. 第二次尝试是使用计时器,该计时器尝试找到要安装的所有nSeconds应用程序。 Sample from another Question: 另一个问题的样本:

public class Example extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");  
        if(installed) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.Ch.Example.pack");
            startActivity(LaunchIntent);

            System.out.println("App already installed on your phone");        
        }
        else {
            System.out.println("App is not installed on your phone");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed ;
    }
}

Last but not least: If you have the APK and root, you can install it using the shell command: 最后但并非最不重要的一点:如果您拥有APK和root,则可以使用shell命令安装它:

pm install APKFile.apk

Try something like this, which will open the Google Play details page for a specific app. 尝试类似的方法,这将打开特定应用程序的Google Play详细信息页面。 (Presuming this is what you want to do) (假设这是您想要做的)

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);

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

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