In the documentation the code snippet to display leaderboard is
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), LEADERBOARD_ID), REQUEST_LEADERBOARD);
This goes into the given leaderboard with LEADERBOARD_ID
My game has severel leaderboards and what I want to do is display a list of them so that user can select a specific leaderboard.
Is it possible to do that?
If you want to use the default UI, you can use this:
startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(getApiClient()), REQUEST_LEADERBOARD);
This is the new way of doing it:
Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this)!!)
.allLeaderboardsIntent
.addOnSuccessListener { intent ->
startActivityForResult(
intent,
RC_LEADERBOARD_UI
)
}
companion object{
private const val RC_LEADERBOARD_UI = 9004
}
Related
I am currently trying to use Instagram's Sharing to Stories API according to this documentation
So far, I have made a function that is called by a button press:
private fun shareStory(){
// This code loads the Drawable from Resources:
val resourceId = R.drawable.ayumu_1
val savedImageURI = with(resources) {
Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(getResourcePackageName(resourceId))
.appendPath(getResourceTypeName(resourceId))
.appendPath(getResourceEntryName(resourceId))
.build()
}
// Intent Code:
val storiesIntent = Intent("com.instagram.share.ADD_TO_STORY").apply {
type = "image/jpeg"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("source_application", "com.example.instastorykotlin")
putExtra("interactive_asset_uri", savedImageURI)
putExtra("top_background_color", "#33FF33");
putExtra("bottom_background_color", "#FF00FF")
}
this.grantUriPermission("com.instagram.android", savedImageURI, Intent.FLAG_GRANT_READ_URI_PERMISSION)
this.startActivity(storiesIntent)
}
I have ensured that the image fits the recommended resolution of 640x480. Now when I press the button, the application opens an intent and immediately closes it, as seen here:
Link
I do not know how to proceed on from here, any help would be appreciated.
Is there a way to capture the back button click listener from Google Places Address Search (AutocompleteSupportFragment)
val btnBackClick =
autocompleteFragment?.view?.findViewById(R.id.places_autocomplete_back_button) as androidx.appcompat.widget.AppCompatImageButton
btnBackClick.setOnClickListener {
Log.e("AutoComplete", "Address Search Back")
}
Tried this leading to crash "java.lang.IllegalStateException: Places must be initialized."
I've tried to get the back button the same way you did on my end, but you are right, that View returns null even though it apparently exists: R.id.places_autocomplete_back_button.
Update: At the moment it is not currently possible to get this View, so I recommend you file a feature request for this in Google's Issue Tracker in case Google Engineers are able to consider adding this capability.
Hope this helps!
I had the same problem. I couldn't get a reference through neither the back button of the AutocompleteSupportFragment / onBackPressed listener / onBackPressedDispatcher from the activity, so I went on and used the error status instead.
// Assuming "autocompleteFragment" is the view name in your XML file
val fragment = supportFragmentManager.findFragmentById(R.id.autocompleteFragment) as AutocompleteSupportFragment
// ...
fragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
// Handle the result like usual
}
override fun onError(status: Status) {
// Check if the user tapped the back button
if (status == Status.RESULT_CANCELED) {
// Do what you want to do when back button is pressed
}
}
}
Technically a workaround, but it does capture the back button flow.
In my Application when a Button is clicked it redirects the user to Google Maps App on the mobile using the following code
Uri gmmIntentUri = Uri.parse("google.navigation:q=" + a + "," + b);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
now What I want is to when the user closes the Google Maps/ returns to My app, it should start a different activity rather than the one it was left on.
Is it possible to do this? I've used a delay to start the secondary activity but it's not giving the result I need as sometimes it runs over the Google Maps app.
Im fairly new to Android studio altogether.
Edit- is onSaveInstanceState is a possible way to overcome this issue?
In Google documentation there is a method: startActivities(Intent[] intents, Bundle options)
I personally have never used it before, but seems to be what you want.
Here is the code. I tried it and it worked. It is in Kotlin, but you can re-write it in Java. Note: Make sure you start the map activity as the second in the array.
val gmmIntentUri = Uri.parse("google.navigation:q=$a,$b")
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps")
val i = Intent(this, SecondActivity::class.java)
val intents = arrayOf(i, mapIntent)
startActivities(intents, null)
There could be couple approaches to solve that issue. For example by using static Map<K,O> and monitoring Activity lifecycle. But because static member should be handled properly to avoid leaking why not use SharedPreferences.
For example in above part of code where you start Google Maps you can save status in SharedPreferences just call:
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", true)
.apply();
And when Google Maps are launched your app will go in onPause state so after user closes the Google Maps your app will be resumed so onResume method will be called there you can check status:
#Override
protected void onResume() {
super.onResume();
boolean isComingFromMaps = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("maps", false);
if(isComingFromMaps) {
//Launch your activity here
//also don't forget to save value back to false to avoid bug when next time Activity is started
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", false)
.apply();
}
}
Also last checking is to make sure if user doesn't return from maps rather he minimises both apps, or kill them you want to set value back to false before super.onDestroy() is called. Like:
#Override
protected void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", false)
.apply();
super.onDestroy();
}
I am using ExoPlayer (https://github.com/google/ExoPlayer) and custom notifications.
I want to access my music player from lock screen and headphone like in google play music and wync.
Please help me.
For playback controls on the lock screen you need to do a MediaStyle notification.
If you want to have an artwork as the lockscreen background you need to support MediaSession and maintain the metadata of the session properly:
new MediaMetadata.Builder(track)
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap)
.putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON, bitmap)
.build();
I am also using Exoplayer with PlayerNotificationManager, And I used MediaSessionConnector and TimelineQueueNavigator to build notifications for the lock screen and background image for the lock screen.
Here, is my question with the Exoplayer team which is resolved for android 11 and above, regarding how to use MediaSessionConnector:
Why PlayerNotificationManager not showing Notification on startForeground in Android 11(R)?
Now, I just added a bitmap (using putParcelable()) for the current session track for MediaDescriptionCompact which will be set to the current MediaSession internally.
Here is the code for that:
val mediaSession = MediaSessionCompat(serviceContext, "DPS_APP")
mediaSession.isActive = true
mediaSessionConnector = MediaSessionConnector(mediaSession).also {
it.setQueueNavigator(
object : TimelineQueueNavigator(mediaSession) {
override fun getMediaDescription(
player: Player,
windowIndex: Int
): MediaDescriptionCompat {
val data: MediaMetaData =
getEmptyOfNullMedia(player)
isBitmapAvailable(getCurrentMediaArt(data))
val extras = Bundle().apply {
putString(
MediaMetadataCompat.METADATA_KEY_TITLE,
getCurrentTitle(data)
)
putString(
MediaMetadataCompat.METADATA_KEY_ARTIST,
getMediaTitle(data)
)
putParcelable(
MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
sessionCurrentBitmap
)
}
return MediaDescriptionCompat.Builder()
.setIconBitmap(sessionCurrentBitmap)
.setExtras(extras)
.build()
}
it.setPlayer(mPlayer)
}
Here is a small brief on that how to refresh or invalidate MediaSession in the given below issue on GitHub:
https://github.com/google/ExoPlayer/issues/5494
I want to show leaderboard score directly as player press on leaderboard icon as like this:
When I was showing a leaderboard it just displaying, list of leaderboard. I want to see actual score directly.
Here is my code:
public void ShowLeaderboard ()
{
if (Social.localUser.authenticated)
Social.ShowLeaderboardUI ();
else {
// authenticate user:
Social.localUser.Authenticate ((bool success) => {
// handle success or failure
if (success) {
Debug.Log ("Login Success....");
PostHighScoreOnLeaderBoard();
Social.ShowLeaderboardUI ();
} else {
Debug.Log ("Login Failed....");
}
});
}
}
How can I show directly a players score?
If you wish to show a particular leaderboard instead of all leaderboards, you can pass a leaderboard ID to the method. This, however, is a Play Games extension, so the Social.Active object needs to be cast to a PlayGamesPlatform object first:
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
...
// show leaderboard UI
PlayGamesPlatform.Instance.ShowLeaderboardUI("Cfji293fjsie_QA");
If you would like the show the user's score directly like in your first image, you need to call PlayGamesPlatform.Instance.ShowLeaderboardUI("leaderboard_id") with the leaderboard ID passed in instead of Social.ShowLeaderboardUI().