0

I have a Switch button which where trying to make active will check if no application is as default open the Action.CONTENT_VIEW and ask for making my application as default browser but only when click always.

If another browser is as default then open a dialog which has some steps and send me to the clear defaults of another app which is as default browser and only if the user clear the default my application will be as default browser.

The problem is that it doesn't check if another app is as default browser or not.

I am based on this answer it helps me little bit but not at all.

https://stackoverflow.com/a/47162845/9560126

Another code which I saw is in this link.

https://stackoverflow.com/a/23622337/9560126

But the problem is that it opens direct the app it does not go to the application settings to clear defaults

Below you can find the code.

public class SettingsActivity extends AppCompatActivity  implements DialogInterface.OnClickListener {

switcherDefaultBrowser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!switcherDefaultBrowser.isChecked()) {
                openDialog();

            } else if (switcherDefaultBrowser.isChecked()) {
                isMyLauncherDefault(getApplicationContext);
            }
        }
    });

  private void openDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setNegativeButton(R.string.Cancel,  this);
    dialogBuilder.setTitle(R.string.SVDialogDefaultBrowserAppTitle);
    dialogBuilder.setMessage(R.string.SVDialogDefaultBrowserAppMessage);
    dialogBuilder.setPositiveButton(R.string.SVDialogDefaultBrowserAppBtnClear,  this);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            switcherDefaultBrowser.setChecked(true);
        }
    });
    alertDialog.show();
}



@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == -2) {
        switcherDefaultBrowser.setChecked(true);
    } else if(which == -1) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
    }

}
boolean isMyLauncherDefault(Context context) {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
    ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(browserIntent,PackageManager.MATCH_DEFAULT_ONLY);
    String defaultBrowserPkg = null;
    if (resolveInfo != null) {
        if (resolveInfo.activityInfo != null) {
            defaultBrowserPkg = resolveInfo.activityInfo.packageName;
            if (!defaultBrowserPkg.equals("searchwith.smartbrowser")) {
                Log.d("TAG", "isMyLauncherDefault: " + defaultBrowserPkg);
                openDialog();
                switcherDefaultBrowser.setChecked(false);

            }
            Log.d("TAG", "isMyLauncherDefault: " + defaultBrowserPkg);
        }
    }
    Log.d("TAG2", "isMyLauncherDefault: " + TextUtils.equals(context.getPackageName(), defaultBrowserPkg));
    return TextUtils.equals(context.getPackageName(), defaultBrowserPkg);
}
}
TheCoderGuy
  • 771
  • 6
  • 24
  • 51
  • possible duplicate: https://stackoverflow.com/questions/3340781/how-to-check-if-my-application-is-set-default-or-not-in-android/3345561 – Christos Themelis Mar 08 '19 at 10:12
  • @ChristosThemelis Can you say to me why you have commented as possible duplicate where I have watched that question ? And my problem is another. – TheCoderGuy Mar 08 '19 at 10:14
  • @Spritzig if you scroll down a bit, someone posted an answer, have you tried that? – node_modules Mar 08 '19 at 10:18
  • @Variable I have watched that to it gave me an idea how to deal with that but the problem it is that the condition only return me false or true. Description of my question is little bit another way. I will edit my question so I will add the code from the answer there. – TheCoderGuy Mar 08 '19 at 10:22
  • @Spritzig because i believe the first answer matches to your question. As I notice, you edited your answer after my comment. Anyway, if this post does not help you, you may have to change the title of your question. Because you getting if you are the default browser or not. But you don't get the package name of another app if is default? Clear that – Christos Themelis Mar 08 '19 at 10:34
  • @ChristosThemelis Yes I have edited my question after saw the answer there. If you want you can edit my question and I can approve that. – TheCoderGuy Mar 08 '19 at 10:37
  • Possible duplicate of [How to find default browser set on android device](https://stackoverflow.com/questions/23611548/how-to-find-default-browser-set-on-android-device) – fantaghirocco Mar 08 '19 at 10:41

0 Answers0