7

Some Android phones don't do anything when the the code below is ran. It's supposed to open the "About device" page in Settings.

For example, I know for a fact that it has no effect on the Huawei Y9 Prime 2019 running Android 10.

startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));

What's the best way to safeguard against this issue when it occurs? (In my app, I open this page to ask the user to perform a specific action there)

Panjeet
  • 117
  • 1
  • 18
  • 2
    Have you tried `startActivityForResult`? – momvart Jul 08 '20 at 16:59
  • @MohammadOmidvar How should I use it elegantly in my case? – Panjeet Jul 08 '20 at 17:02
  • I haven't faced the same issue before, but as there are many devices with incompatibilities (especially among Huawei ones), you can always check for this kind of stuff to see if the problem is solved. – momvart Jul 08 '20 at 17:06
  • 3
    From the docs: `In some cases, a matching Activity may not exist, so ensure you safeguard against this.` here: https://developer.android.com/reference/android/provider/Settings#ACTION_DEVICE_INFO_SETTINGS – M D P Jul 09 '20 at 09:14
  • @MDP I'm aware of this. What I'm asking for is the best way to safeguard against this issue when it happens. – Panjeet Jul 09 '20 at 10:59
  • The remedy would be checking for the device manufacturer and using that manufacturer's custom APIs. For example: `com.huawei.hms.location.SettingsClient` for accessing location on Huawei devices – M D P Jul 09 '20 at 11:11

2 Answers2

3

Use PackageManager.resolveActivity() to check whether such an Activity exists. If that returns null, there is no activity that matches the Intent and you have to guide your customers to the settings in another way:

Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, 0);
if (resolveInfo == null) {
    // help customers by describing how to get to the device info settings

} else {
    startActivity(intent);
}
Bram Stoker
  • 1,202
  • 11
  • 14
  • How can I test this code to make sure it works? I don't have a Huawei phone unfortunately. – Panjeet Jul 09 '20 at 12:21
  • That's the difficult part as there are so many Android devices and manufacturers. Google did think about this: https://firebase.google.com/docs/test-lab – Bram Stoker Jul 09 '20 at 12:41
  • OK. I just tested this out and the 'About device' page opens normally on the available Huawei devices. They don't have the Huawei Y9 Prime 2019 :( So I wasn't able to verify if your code works. – Panjeet Jul 09 '20 at 14:24
  • Maybe Huawei can provide some test phones: https://stackoverflow.com/a/59316655/4706541 – Bram Stoker Jul 09 '20 at 21:13
  • They need ID verification to create a Huawei developer account. Due to privacy concerns, I can't provide them with it. – Panjeet Jul 11 '20 at 15:10
  • How can I emulate an activity that doesn't exist to test your code? Do you have an example of an activity that doesn't exist? – Panjeet Jul 14 '20 at 21:48
  • You can't, you need Huawei's OS in order to test it, but Huawei does not provide any images to setup an emulator. Using their cloud debugging service is your best option or you need to find a friend with a Huawei Y9 Prime 2019. – Bram Stoker Jul 15 '20 at 06:31
  • That's not what I meant. What I meant is: are there any activities that are known not to exist on Samsung devices for example? That way, I can test your code on my Samsung device. – Panjeet Jul 15 '20 at 10:26
  • That's easy, you can make up an action like `Intent("com.company.app.ACTION_THAT_DOES_NOT_EXIST")` that's guaranteed not to find a matching `Activity` – Bram Stoker Jul 15 '20 at 11:07
  • I tried `new Intent(Settings.ACTION_INEXISTENT_ACTIVITY);` but Android Studio doesn't let me build. Can you tell me step by step what I need to do? – Panjeet Jul 15 '20 at 11:55
  • 1
    Because that field does not exist in `Settings`, that's why I put in a `String` of my own (of a non-existent action). Read through the official docs to better understand Intents and Intent Filters: https://developer.android.com/guide/components/intents-filters – Bram Stoker Jul 15 '20 at 12:43
0

If an activity is not found, the method startActivity will throw android.content.ActivityNotFoundException. You should catch this exception and notify the user:

try {
    startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
} catch (ActivityNotFoundException e) {
    // Notify the user, eg. using a popup so they can open settings manually 
}

Hannes Hertach
  • 499
  • 5
  • 23