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