Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android openURI(...) not working on Android 11 #6377

Closed
5 tasks
noblemaster opened this issue Jan 28, 2021 · 11 comments · Fixed by #6408
Closed
5 tasks

Android openURI(...) not working on Android 11 #6377

noblemaster opened this issue Jan 28, 2021 · 11 comments · Fixed by #6408
Labels
android bug net concerns Gdx.net

Comments

@noblemaster
Copy link
Member

noblemaster commented Jan 28, 2021

Issue details

Android's openURI is not working on Android 11 anymore. Calling openURI(...) does nothing. There is no error thrown.

Version of LibGDX and/or relevant dependencies

Latest stable release 1.9.13. The Android project has to be set to Android 11 (30).

Suggestion for Code Fix

A quick test showed the following code works. The live wallpaper flag needs to be set. Also, does this really need to run via UI thread as in libGDX's example?

try {
   // open with the correct activity if any 
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
   return true;
}
catch (Exception e) {
   // ... log error...
   return false;
}

Please select the affected platforms

  • [x ] Android 11
  • iOS
  • HTML/GWT
  • Windows
  • Linux
  • MacOS
@MrStahlfelge MrStahlfelge changed the title Android openURI(...) now working on Android 11 Android openURI(...) not working on Android 11 Jan 28, 2021
@MrStahlfelge MrStahlfelge added android bug net concerns Gdx.net labels Jan 28, 2021
@MrStahlfelge
Copy link
Member

Just tested on an R emulator, it still works for me. The only difference in your code and the backend code I spotted is the check if there is a handler for the URI.

You mention something about live wallpapers, how does that relate to opening an URI?

@noblemaster
Copy link
Member Author

I updated the comment. I believe this happens only if the libGDX project is set to Android 11 (30). I didn't have the problem before but changing to SDK 30 makes the problem occur. Unless there is something else bad happening in my project.

@MrStahlfelge
Copy link
Member

MrStahlfelge commented Jan 30, 2021

Checked it, it is working for me with targetSdk 30 on Android R emulator

@MobiDevelop
Copy link
Member

What type of uri is it? Just a normal web url?

@noblemaster
Copy link
Member Author

Yes, it's normal http://... URL. It was working fine before with targetSdk 29. One of the players reported it on Android 11 (30) after I used targetSdk 30 for the build. The URLs still open on my Android 9 with targetSdk 30.

Maybe the PackageManager.MATCH_DEFAULT_ONLY could cause problems if there is no default browser set? Maybe there is something else I did that makes openURL not work anymore?

I was able to reproduce it on a fresh emulator for Nexus 6P and SDK 30.

image

@MrStahlfelge
Copy link
Member

Can you check by stepping through it with the debugger? You should be able to see if the command is not executed or if it fails.

@MobiDevelop
Copy link
Member

MobiDevelop commented Jan 31, 2021

Using a Pixel emulator on Android 11, with the Android tests set to target API 30, this is reproducible via NetAPITest.

Android 11 added some 'privacy' around package visibility, which requires specifying in the AndroidManifest.xml the type of intent queries you will perform - the backend code performs a query to see if any activity can resolve. Adding this to the AndroidManifest.xml makes openURI work:

	<queries>
		<intent>
			<action android:name="android.intent.action.VIEW" />
			<data android:scheme="http" />
		</intent>
	</queries>

The reason it works with the proposed solution is that you are not querying for installed packages and the system makes an exception in the case where you just fire off a startActivity call - this is not without some trouble though as there is some very strange behavior that comes along with this for some types of URIs.

@noblemaster
Copy link
Member Author

@MobiDevelop Thanks, I wasn't aware of that! I guess this works. Also, I am using mailto for email which I probably would have to add as well. What side effects of directly calling startActivity are you referring to?

@MrStahlfelge Good point, I should have debugged that further myself.

Adding <queries> to the AndroidManifiest.xml is doable. But what about other libGDX users that forget to do that? Are there other URIs besides http and mailto that might be needed? I think the simple solution is to simply call startActivity directly and return the results depending if we get an exception or not.

@MobiDevelop
Copy link
Member

The system behaves oddly, at least it did for me with a geo uri in the app I work on at my day job - it never offered the chooser despite multiple apps being installed until I added the queries element. Whichever the last app was that could handle that uri was chosen (in our case, it was Uber) automatically. Not sure if other types of uris behave the same way.

@MrStahlfelge
Copy link
Member

MrStahlfelge commented Feb 7, 2021

I was able to reproduce this with a fresh emulator, too. The code @noblemaster gave in the OP works for me, too, or a slightly different variant:

	public boolean openURI (String URI) {
		final Uri uri = Uri.parse(URI);
		try {
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			// LiveWallpaper and Daydream applications need this flag
			if (!(app.getContext() instanceof Activity)) {
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			}
			app.startActivity(intent);
			return true;
		} catch (ActivityNotFoundException e) {
			return false;
		}
	}

According to stackoverflow, using the queries is needed when you need to know if there are applications, but not start them. We don't have the situation here, and I am sure runOnUiThread is not needed. It was not introduced because of crash reports but from the beginning, probably out of precaution (5ef9a9e)

@MrStahlfelge
Copy link
Member

Ah found it, it is not precaution but to make openUri threadsafe: #59 (comment)
I don't see much benefit in having a UI operation threadsafe though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android bug net concerns Gdx.net
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants