Install app if it does not exist on the smartphone

Asked

Viewed 35 times

1

I developed an app that needs to open the file manager, on 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 asked and ask if I want to install it.

follows below the code snippet 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);
}

1 answer

0

Good afternoon!

After some researches I managed to solve my own questioning.

follows 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 I can help someone with the same doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.