android TV mediacontroller - android

I try to use android's mediabrowser and sample by google works fine on phones (https://github.com/googlesamples/android-media-controller)
But on android TV it can't find any app which supported this feature
final Intent mediaBrowserIntent =
new Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE);
final List<ResolveInfo> services =
packageManager.queryIntentServices(mediaBrowserIntent,
PackageManager.GET_RESOLVED_FILTER);
in services placed only current app
Could you help me get API to collect last used mediadata? I can't find any description or guide for creating TV launcher (which have access to last used or recommended media by another app)
I just wanna GET metadata like https://developer.android.com/training/tv/playback/now-playing.html
or
http://corochann.com/android-tv-application-hands-on-tutorial-11-277.html

You can query for all active media sessions using the method MediaSessionManager.getActiveSessions(notificationListnerComponentName) which will return a list of alla ctive mediacontrollers with the most active as the first item in the list.
Code sample:
MediaSessionManager mediaSessionManager = context.getSystemService(Context.MEDIA_SESSION_SERVICE);
List<MediaController> activeMediaSessionControllers = mediaSessionManager.getActiveSessions(null);

Related

Open media output picker programmatically

In Android 9 media output controls were added to the volume selection dialog.
I would like to have a button in my app which would open these system media output controls so that the user can choose where the audio will be played.
I could find nothing about this in: Android 9 features and APIs, Behavior changes: apps targeting API level 28+ and Behavior changes: all apps
You are looking for MediaRouter API.
public class MediaRouterPlaybackActivity extends AppCompatActivity {
private MediaRouteSelector mSelector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a route selector for the type of routes your app supports.
mSelector = new MediaRouteSelector.Builder()
// These are the framework-supported intents
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.build();
}
}
For further reference: https://developer.android.com/guide/topics/media/mediarouter#java
Also check Create a MediaRouteSelector section.
I did some research about this as well so I know that Android Pie is capable of this, but they haven't provided a way for us developers to do it yet (at least I haven't found a way). The CATEGORY_LIVE_AUDIO in MediaRouterSelector is also only for secondary output; you can read about it on the developer website. So if you must do it, I think the only way you can do that right now is to do a workaround programmatically:
Disconnect current device (The important of this step depends on the device; I have to do this on my Essential phone)
Use BluetoothAdapter.getBondedDevices() to get a list of paired devices.
Connect to the one you want
Try this - it works for me on Android 11:
Intent intent = new Intent("com.android.settings.panel.action.MEDIA_OUTPUT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Google Play Games Service - Get List of running matches

I'm working on a TurnBased Game with the Google Play Games Service.
Now I would like to list all Games you are involved in the DrawerMenu so you could enter the games very easy and also see what games you are in...
My Question is, how do I get these Games and also how do I get them so I can reenter them on a click?
Hope I could make my Problem clear :)
Thank you!
This is how I did it:
static final int[] statusesToLoad = new int[]{
TurnBasedMatch.MATCH_TURN_STATUS_INVITED,
TurnBasedMatch.MATCH_TURN_STATUS_MY_TURN,
TurnBasedMatch.MATCH_TURN_STATUS_THEIR_TURN,
TurnBasedMatch.MATCH_TURN_STATUS_COMPLETE};
PendingResult<TurnBasedMultiplayer.LoadMatchesResult> loadMathesResult = Games.TurnBasedMultiplayer.loadMatchesByStatus(mGoogleApiClient, statusesToLoad);
loadMathesResult.setResultCallback(new loadMatchesCallback());
And then in the callback you can get data buffers for all the active games you are involved in and all the invitations you received:
private class loadMatchesCallback implements ResultCallback<TurnBasedMultiplayer.LoadMatchesResult>{
#Override
public void onResult(TurnBasedMultiplayer.LoadMatchesResult loadMatchesResult) {
LoadMatchesResponse response = loadMatchesResult.getMatches();
TurnBasedMatchBuffer myTurnMatches = response.getMyTurnMatches();
TurnBasedMatchBuffer theirTurnMatches = response.getTheirTurnMatches();
TurnBasedMatchBuffer completedMatches = response.getCompletedMatches();
InvitationBuffer invitations = response.getInvitations();
}
}
}
You can use these buffers just as you use ArrayList, but you need to keep in mind that google wants you to release() them after you are done. If you won't you're gonna get a notification in your logs about a data leak.
You should use this method:
Games.TurnBasedMultiplayer.getInboxIntent(GoogleApiClient apiClient);
According to the documentation of the method, this method returns an Intent that can be used to start an activity that shows invitations and matches.
Hope this helps. Any other questions feel free to comment and I'll try to answer.
in iOS it is like this:
this one gets all matches where you have been invited and are waiting for you to answer, you can then change the GPGTurnBasedUserMatchStatusInvited to another type with autocomplete.
GPGTurnBasedModel *turnModel = [GPGManager sharedInstance].applicationModel.turnBased;
NSArray *invited = [turnModel matchesForUserMatchStatus:GPGTurnBasedUserMatchStatusInvited];
there is also a function to get ALL the matches but it includes the finished ones, maybe you don't want that.

How to use sendintent in google tv remote

I'm developer about android in Korea.
Currently, I have developed about Google TV with smart phone.
I have one problem, so I enter this site.
The problem is using AnymoteSender in Google TV remote.
The blackjack project does connect smart TV with smart phone.
So, It project using AnymoteSender library and use sendIntent with component.
for example)
final Intent TVLaunchIntent = new Intent("android.intent.action.MAIN");
TVLaunchIntent.setComponent(new ComponentName(
"com.tvstorm.sportscompanion.tv",
"com.tvstorm.sportscompanion.tv.SportsCompanionGTVActivity"));
anymoteSender.sendIntent(TVLaunchIntent);
anymoteSender have sendIntent function.
but How about Google TV Remote project?
It use DeviceAdapter in AnymoteSender that is user-defined class(DeviceAdapter is library).
for example)
public void click(Action action) {
DeviceAdapter sender = getSender();
if (sender != null) {
sender.sendKeyEvent(Code.BTN_MOUSE, action);
}
}
DeviceAdapter hasn't sendIntent function.
So, I thinked two ways.
one) I implemented clintlistener in activity.
But, It didn't bind to AnymoteSender and made some error.
It didn't use bindService. So, It make some error.
two) I make new AnymoteSender like this AnymoteSender = new AnymoteSender();
but It needs ConnectingTask like new AnymoteSender(ConnectingTask task);
and ConnectingTask need tvDevice and keyStoreManger.
like this,
ConnectingTask task = new ConnectingTask(tvDevice, keyStoreManager, context);
So, I connect AnymoteLibrary with my project, TvDevice and KeyStoreManager.
but, The Keystore manager was different between KeystoreManager in Google TV remote project and in Library.
How to connect AnymoteSender in GoogleTV Anymote project with AnymoteSender Library?
So, I want to solve this problem. anyone know this problem, Please reply my question.
Thank you!!

How to detect if an android app is a game?

In an app I am developing I need to iterate through the installed apps and detect which ones are games. Is there any way to do this?
I was thinking to a Play Store API that can search for package name and returns its category even if it's only limited to apps on the store. Does something similar exist? Would it be possible?
Is there any alternative way to do it?
This answer is deprecated!
Correct and backwards compatible way to do this is here!
Since Android API version 21, there's finally a way to check if an application is a game.
PackageManager pm = mContext.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(mPackageName,0);
if((ai.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME)
return true;
return false;
There is no automatical way to detect if an app is a game. You just could compaire the package name of the common part of the package name. My solution was to index the google store pages and hash the package names.
I could optimize my hashes by building common prefixes. I handled the package name as a domain and grep the public suffix. I use the list from http://publicsuffix.org/.
A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are .com, .co.uk and pvt.k12.ma.us. The Public Suffix List is a list of all known public suffixes.
The Public Suffix List is an initiative of Mozilla, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.
With this list you can detect part of a packagename is a common prefix.
For me the above answer didn't work, the ApplicationInfo.FLAG_IS_GAME is now deprecated, with API 28+ (in my case), you can do something like this:
_pm = _context.PackageManager;
List<string> packageList = new List<string>();
Intent intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryLeanbackLauncher); // or add any category you want
var list = _pm.QueryIntentActivities(intent, PackageInfoFlags.MetaData);
foreach (var app in list)
{
ApplicationInfo ai = _pm.GetApplicationInfo(app.ActivityInfo.PackageName, 0);
var allFlags = ai.Flags;
if (allFlags.HasFlag(ApplicationInfoFlags.IsGame))
{
packageList.Add(app.ActivityInfo.PackageName);
}
}

Android: Programmatically open "Recent Apps" dialog

I would like to be able to open the "Recent Apps" dialog from my application. This is the dialog that is opened by long-pressing the home button. I am programming for Android 4.1 or earlier. I found a way to do it by implementing a custom AccessibilityService and calling AccessibilityService.performGlobalAction(GLOBAL_ACTION_RECENTS), but this requires enabling accessibility on the phone, which is not very desirable. Is there any other way to open this dialog from an app?
Thanks for the help!
This code won't work on Nougat or later
It is possible to trigger the recent apps activity.
The StatusBarManagerService implements an public method which you can use through reflection.
You can use the following code:
Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClass.getMethod("getService", String.class);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerClass, "statusbar");
Class statusBarClass = Class.forName(retbinder.getInterfaceDescriptor());
Object statusBarObject = statusBarClass.getClasses()[0].getMethod("asInterface", IBinder.class).invoke(null, new Object[] { retbinder });
Method clearAll = statusBarClass.getMethod("toggleRecentApps");
clearAll.setAccessible(true);
clearAll.invoke(statusBarObject);
Have fun
You cannot access that. However, it isn't super hard to roll your own. The getRecentTasks() method returns a list of recently run apps. Simply take the list and add your own UI to it.
One advantage to this is that the default one, at least on older versions of Android, only shows you about 8 apps. If you roll your own can show as many as you want.
This can be done using the TOGGLE_RECENTS Intent.
Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setComponent (new ComponentName ("com.android.systemui", "com.android.systemui.recent.RecentsActivity"));
startActivity (intent);
Note Package would be changed basis on Api level. See here.
Android 4.1: "com.android.internal.policy.impl.RecentApplicationsDialog"
Android 4.2 - 4.4: "com.android.systemui.recent.RecentsActivity"
Android 5.0 - 7.1: "com.android.systemui.recents.RecentsActivity" ("s" letter was added)

Categories

Resources