I've came across some radio app's that share a common, how can I say, way to implement their functionality, and (since I'm new on android dev) I'm really curious about it.
I'll use some images from the app "Simple Radio" to illustrate my doubt.
So, in what I call the main activity, you simply have a feed of radios from where you can choose one to play.
If no radio is playing/stopped on background, what you see when a radio is choosen is this activity, the one I call the streaming activity:
However, if one radio was playing/stopped on background, and you choose another one, what you'll see is this toolbar in the end of the feed:
So.. I know there's a background service for streaming the choosen radio, but how do they manage to control the radio both on the main and streaming activity? Is that possible because services (and it's current state) can be accessed from anywhere or something?
I know there's a background service for streaming the choosen radio, but how do they manage to control the radio both on the main and streaming activity? Is that possible because services (and it's current state) can be accessed from anywhere or something?
The background service is actually a background Service, an Android controller that is independent of activities.
This controller runs until it is told to stop, and has nothing to do what is visually on screen. The visual components can communicate with the Service to send instructions to it and vice versa.
Related
I created a background service on android and I have two buttons which appear on the top of the screen all the time. I want to use these two buttons like scroll down and scroll up. But these two buttons should work on any kind of applications like Instagram, Facebook, Twitter and so. So, it means it should work in all applications that use scrolling.
I search a week on internet but I could not find any solutions.
This is not possible, sorry. Something like this would require your Service to have access to the Views of the applications and this would be a huge security breach, because you could read values from them and so on.
You could achieve this with a custom button code broadcast (so basically your buttons would act as physical buttons on the device) but this would most probably require you to have system-level permissions and some level of cooperation with the OEMs.
Android Activity class has a method called dispatchKeyEvent(), which could let you simulate the key input (with some limitations) but this is not present in the Service class.
Sadly this is not something you can do in Android. Typically you should not be able to touch views with a background service, the point of a background service is that you do some work in it (for example upload files to your web server or get some data). You CAN send a signal from a service once you're finished doing work to tell an app that something needs to happen, however the app needs to be specifically coded to respond to this broadcasted event.
If you wanted to do this with an app that you have developed, that can be achieved by using the onReceive method of say a BroadcastReceiver, however you cannot specifically define the behaviour of other apps as this would represent a security breach in Android.
I'm developing a chromecast streaming app for Android. In Activity A the user chooses between different playable items and then is moved to Activity B, which holds the Chromecast controls. When the user hits the back button he is presented with Activity A from where he can choose another video while the casting of the first one is still running on the TV. The problem is that after the back button is pressed, Activity B which holds the controls (for example: seek bar with the video progress) is destroyed and there is no way the user can interact with the destroyed controls anymore. Is there a way I can keep Activity B alive? I'm a novice developer and I have no experience with fragments, so I'm looking for a solution which does not involve their usage...
If you want to use the streaming without any UI showing, you need to create a Service and bind to it with your activities. Let the service handle everything. Like this you're not dependent on the UI being shown.
I strongly suggest you read the Cast UX Checklist where this issue is brought up and addressed. The ways to address that are:
Having a persistent controller on each page
Using the Media Router Controller Dialog when casting
When not in the application but casting, using Notification
When on the lock screen, using the Lock Screen media controller buttons
On Android, you can use the CastCompanionLibrary that provides all of these for you with minimal coding.
I want to start chromecast routing automatically and not when the user presses the button. Does anyone know how i can simulate in any way that the user pressed the media route button? I have looked through the different classes and not found anything.
I am aware that this is not how Google intends developers to use it, and my application is only functioning as a proof of concept.
If anyone knows another way to achieve the same thing (The casting starts when the app starts, if the user has enabled it in the options menu) - let me know!
You can follow the same steps as usual (get a hold of MediaRouter instance, set a selector, register a callback, etc) but then you need to keep a list of discovered routes in your application (as they are discovered by MediaRouter; you will get a call back via onRouteAdded(()). You need to do the bookkeeping as well (via onRouteRemoved() callback). Now that you have a list of routes, you can programmatically decide which one is the one you want to use and again do as usual (same stuff that you would do when you get a callback via onRouteSelected()) except that you need to call MediaRouter.selectRoute(your_selected_route) yourself to tell the framework about it. For the first part, you can take a look at this sample.
So what I discovered was that I couldn't make a check for routes in the beginning of the program because the MediaRouter hadn't discovered them yet. (I.e the call to getRoutes returned only the default route...) In my program, it was enough to start a thread that sleeps for three seconds and then calls selects any available route:
if(mMediaRouter.getRoutes().size() >= 1) {
mMediaRouter.selectRoute(mMediaRouter.getRoutes().get(1));
}
If I needed a more persistent solution, I'd do as Ali Naddaf suggested.
I'm trying to figure out the right way to add Chromecast buttons (pause, play, etc) to an Android Notification. I've set up a custom notification that sends PendingIntents to a ChromecastService. That service is trying to interact with a class I built called ChromecastAdapter. The ChromecastAdapter implements MediaRouteAdapter and contains all the listeners and state that go along with casting. However, all this state is gone as soon as I exit the application. So, my ChromecastService doesn't end up having access to the Chromecast once my app is gone.
It seems to me that the only way to get this to work is refactor all the Chromecast state into a Service that implements MediaRouteAdapter. I really don't want to do this since I'm pretty happy with the way things are now.
Since these interactive Notifications are required by Google, I feel like there has to be a standard way of interacting with a cast from a Notification. Am I on the right track here? Do I have to place all my Chromecast interactions behind a Service?
What the behavior should be depends on the type of app and the requirements of the app. If your app is "gone" (in the sense that the Application instance is gone), then the question that you should ask yourself is whether you would want to keep a notification mechanism to stay around; there are apps that when they are killed, the receiver also gets closed and user is sent back to the home screen on the chromecast device, in which case there is no reason to keep a notification around.
On the other hand, there are apps that based on their requirements, you would want to let the cast device continue what it was doing (for example play the video) even if the mobile app is gone. In those cases, you may want to have a notification mechanism in place for "bringing up" the app. To achieve that, you need to maintain certain amount of information/state/objects in a service, enough to be able to establish a connection again and "join" the running app. In addition, your "service" needs to be aware of the status of the app on your receiver so if that app is killed (say, someone else starts casting a different app to the device), it can be notified and exit.
Alright, so I'm writing an app to make an external headset perform certain functions. I'm trying to replicate functionality similar to this guy:
https://market.android.com/details?id=com.kober.headset
The list of things it can make the headset button click do is: play/pause, previous track, redial last phone #, launch specified app, etc. I don't know how to do this stuff though! Sort of lost with how to get started here. I'm also kind of confused how the overall program flow should work.
Right now I can register button clicks via a broastcast receiver and correctly and pass that number (single/double/triple click) to a service to run in the background. I'm using a background service so the user doesn't need to be in the app to use the functionality.
Am I on the right track? How do I go about making clicks play/pause music, next/previous track, etc? Are there certain intents I should be using?
Cheers.
Am I on the right track?
Be sure to stop your service when it is no longer needed (e.g., after you have determined the clicks and performed the operation). Otherwise, I see no problems there.
How do I go about making clicks play/pause music, next/previous track, etc?
First, write a music player app. There are no documented and supported APIs to "play/pause music, next/previous track, etc" for most of the several thousand music players out there.