Install app if it does not exist on the smartphone

1

I developed an app that needs to open the file manager, in my device it works perfectly, but in others it did not work, because the manager is not installed, I would like the app itself to identify that the manager is not prompted and ask me if I want install it.

Below is the section of code that opens the manager.

Uri uri = Uri.parse(Envrronment.getExternalStorageDirectory().getPath() + "/Prova/");
PackageManager packageManager = "com.estrongs.android.pop";   
Intent intent = packageManager.getLaunchIntentForPackage(packageManager);

intent.setDataAndType(uri, "aplication/vnd.ms-excel");
if(null!=intent){

    startActivity(Intent);
}
    
asked by anonymous 11.07.2018 / 00:34

1 answer

0

Good afternoon!

After some research I was able to resolve my own questioning.

follow the code.

public void uri_intent(){

    PackageManager pm = getPackageManager();
    try {

        if (pm!=null){ // PRIMEIRA CONDIÇÃO, SE NÃO EXISTIR PASSA PARA A SEGUNDA

            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/.Prova/");
            PackageManager packageManager_a = getPackageManager();
            String packagename_a = "com.sec.android.app.myfiles";
            PackageInfo info = pm.getPackageInfo("com.sec.android.app.myfiles", PackageManager.GET_META_DATA);
            Intent intent_a = packageManager_a.getLaunchIntentForPackage(packagename_a);
            intent_a.setDataAndType(uri, "application/vnd.ms-excel");
            startActivity(intent_a);

        }else if (pm!=null){ // SEGUNDA CONDIÇÃO, SE NÃO EXISTIR PASSA PARA A TERCEIRA

            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/.Prova/");
            PackageManager packageManager_a = getPackageManager();
            String packagename_a = "com.estrongs.pop";
            PackageInfo info = pm.getPackageInfo("com.estrongs.pop", PackageManager.GET_META_DATA);
            Intent intent_a = packageManager_a.getLaunchIntentForPackage(packagename_a);
            intent_a.setDataAndType(uri, "application/vnd.ms-excel");
            startActivity(intent_a);

        }else if (pm!=null){ // TERCEIRA CONDIÇÃO, SE NÃO EXISTIR NENHUMA DAS TRÊS VAI CHAMAR OUTRA CONDIÇÃO PEDINDO PARA INSTALAR

            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/.Prova/");
            PackageManager packageManager_a = getPackageManager();
            String packagename_a = "com.lenovo.FileBrowser2";
            PackageInfo info = pm.getPackageInfo("com.lenovo.FileBrowser2", PackageManager.GET_META_DATA);
            Intent intent_a = packageManager_a.getLaunchIntentForPackage(packagename_a);
            intent_a.setDataAndType(uri, "application/vnd.ms-excel");
            startActivity(intent_a);
        }

    } catch (PackageManager.NameNotFoundException e) {

        AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);
        dialogo.setTitle("Erro");
        dialogo.setIcon(R.drawable.error);
        dialogo.setMessage("Aplicativo não instalado, deseja intala-lo agora?");
        //dialogo.setNeutralButton("Ok", null);
        dialogo.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.sec.android.app.myfiles")));
            }
        });
        dialogo.show();
    }
}

I hope to help someone with the same doubt.

    
11.07.2018 / 20:01