Opening privacy settings on Android phone programmatically

JohnHardmanJohnHardman GBUniversity mod
edited October 2017 in Xamarin.Forms

When I execute the following code on my Samsung phone, with action set to Android.Provider.Settings.ActionPrivacySettings, rather than the Privacy settings page opening, the "Backup and reset" page opens. Any ideas why it might open "Backup and reset"?

        Intent intent = new Intent(action);
        ComponentName componentName = intent.ResolveActivity(Forms.Context.PackageManager);

        if (componentName != null)
        {
            MainActivity.TheInstance.StartActivity(intent);
        }

Best Answer

  • JGoldbergerJGoldberger US Xamurai
    edited September 2018 Accepted Answer

    @JohnHardman

    It looks like there is nothing you can do here. According to the Android source code, the Backup and Reset Settings page responds to "android.settings.PRIVACY_SETTINGS" and "android.settings.BACKUP_AND_RESET_SETTINGS" intent. See the intent flags in the link.

           <activity android:name=".backup.BackupSettingsActivity"
                  android:label="@string/privacy_settings_title"
                  android:icon="@drawable/ic_settings_backup"
                  android:taskAffinity="com.android.settings"
                  android:parentActivityName="Settings">
            <intent-filter android:priority="1">
                <action android:name="android.settings.PRIVACY_SETTINGS" />   <!-- Here the back up and reset settings page is set to handle the Privacy Settings intent -->
                <action android:name="android.settings.BACKUP_AND_RESET_SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            ...
        </activity>
    

    So there is no error and nothing you can check since the privacy settings intent is handled correctly as far as the Android OS is concerned. But you can check to see if an action intent will be handled or not with this code:

    var activities = PackageManager.QueryIntentActivities(intent, 0).ToList();
    

    If you get any results, then there is an activity that can open the intent. And in this case there is an activity that can open the intent as far as the OS is concerned, even if "these are not the droids you are looking for." :-)

    Back in Froyo, there was a privacy settings page that could open with your intent and the title was privacy settings.

    For Oreo, I do also see a PrivacySettings activity in the settings manifest, but it does not handle any of the action intents.

    In Nougat, there is no Backup and Reset activity in the manifest
    but the privacy settings activity handles the same two intents, i.e. "android.settings.PRIVACY_SETTINGS" and "android.settings.BACKUP_AND_RESET_SETTINGS" but you can see that the title for that activity is backup and reset.

    So you can check for OS version number to know what settings pages are available.

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    I saw the same thing happened on an emulator, but if you read the google docs on constant ACTION_PRIVACY_SETTINGS (returns string "android.settings.PRIVACY_SETTINGS" a from Androidsame as from Provider.Settings.ActionPrivacySettings), you will see this:

    Activity Action: Show settings to allow configuration of privacy options.
    In some cases, a matching Activity may not exist, so ensure you safeguard against this.

    So I see on the emulator I am using, there is no privacy settings page, but there is a security settings page and I can open that with Android.Provider.Settings.ActionSecuritySettings. So make sure your device has Privacy settings page. My guess would be that there was a privacy page on older versions of Android, but it was deprecated.

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 2018

    @JGoldberger said:

    Activity Action: Show settings to allow configuration of privacy options.
    In some cases, a matching Activity may not exist, so ensure you safeguard against this.

    Thanks Jon. On this particular device, switching to using Android.Provider.Settings.ActionSecuritySettings does the trick. But how can I programmatically identify whether I need Android.Provider.Settings.ActionPrivacySettings or Android.Provider.Settings.ActionSecuritySettings? Using Android.Provider.Settings.ActionPrivacySettings on that device, the intent is created successfully, the activity is resolved and started, all without reported error or exception, even though it then ends up on the wrong settings page.

                Intent intent = new Intent(action);
                ComponentName componentName = intent.ResolveActivity(Forms.Context.PackageManager);
                MainActivity.TheInstance.StartActivity(intent);
    
  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited September 2018 Accepted Answer

    @JohnHardman

    It looks like there is nothing you can do here. According to the Android source code, the Backup and Reset Settings page responds to "android.settings.PRIVACY_SETTINGS" and "android.settings.BACKUP_AND_RESET_SETTINGS" intent. See the intent flags in the link.

           <activity android:name=".backup.BackupSettingsActivity"
                  android:label="@string/privacy_settings_title"
                  android:icon="@drawable/ic_settings_backup"
                  android:taskAffinity="com.android.settings"
                  android:parentActivityName="Settings">
            <intent-filter android:priority="1">
                <action android:name="android.settings.PRIVACY_SETTINGS" />   <!-- Here the back up and reset settings page is set to handle the Privacy Settings intent -->
                <action android:name="android.settings.BACKUP_AND_RESET_SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            ...
        </activity>
    

    So there is no error and nothing you can check since the privacy settings intent is handled correctly as far as the Android OS is concerned. But you can check to see if an action intent will be handled or not with this code:

    var activities = PackageManager.QueryIntentActivities(intent, 0).ToList();
    

    If you get any results, then there is an activity that can open the intent. And in this case there is an activity that can open the intent as far as the OS is concerned, even if "these are not the droids you are looking for." :-)

    Back in Froyo, there was a privacy settings page that could open with your intent and the title was privacy settings.

    For Oreo, I do also see a PrivacySettings activity in the settings manifest, but it does not handle any of the action intents.

    In Nougat, there is no Backup and Reset activity in the manifest
    but the privacy settings activity handles the same two intents, i.e. "android.settings.PRIVACY_SETTINGS" and "android.settings.BACKUP_AND_RESET_SETTINGS" but you can see that the title for that activity is backup and reset.

    So you can check for OS version number to know what settings pages are available.

  • JohnHardmanJohnHardman GBUniversity mod

    Many thanks @JGoldberger - excellent answer :-) Also reminds me that I should get familiar with the Android source code - I spend plenty of time in the XF source code, but tend to forget that the Android source code is also freely available.

Sign In or Register to comment.