I have been developing Android application that use Activity with "download" button and Service for executing downloading in the background. And I have following task: to show message about downloading if application is currently displayed. How can I detect it? Is there standard Android OS functions for it? Thank you.
You can do this at the activity level, not for the application as a whole. Check out the description of the activity lifecycle. (Also see "Managing the Activity Lifecycle" in the Activities framework topic.) When your activity becomes visible, the framework will call it's onStart() method. When the user can interact with the activity, the framework will call the onResume() method. You can override one of these to know when your activity is showing or actually interacting with the user.
Note that, as described in the documentation, things work a little differently starting in Honeycomb (3.0).
The basic method is to use Broadcast receiver to share information between a service and an application. If your requirement is to update the status/progress of download on the main activity, you can use it.
Here is a simple tutorial about how to share information between a service and an activity. You can update the progress of download. It works for the activity that is currently running.
To check for it as the activity starts, you should override the onCreate, onStart() or onResume() functions.
Also there is a very neat solution presented in another question and i recommend that you should try to use that. It involves extending the Application class to store the information, and use its instance to check for updates. See this
Related
I'm trying to make a phone call from my app in a way it does not need to switch activities, But every guide I find has the following code snippet,
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:XXXXXXXXX"));
startActivity(callIntent);
which switches the activity I start the call from to another activity (in this case to an activity in a different app). Is there a way to stop this from happening? I managed to do it with a third party library called "sinch" but I'm wondering if there is a native way to do it or maybe a better library?
Ps- the app I'm building it for myself, basically, I'm building a voice assistant that can make calls via voice commands, hence I can't let it switch activities. I have no intention of publishing it on the app store and I have no difficulty giving dangerous permissions :) My plan is to run it on a separate piece of hardware in the future.
This link can help you, but as Xavier Rubio Jansana wrote previously, Google hardly accepts applications that do not use intents to make phone calls :
https://google-developer-training.github.io/android-developer-phone-sms-course/Lesson%201/1_c_phone_calls.html
Google wants any programmer to use an intent to make the user view the default phone application handle the phone call.
If your app does not need to be available on Google Play Store then it would be ok for you to make the phone call directly.
To elaborate on what I was talking about earlier, it is talked about in this stack overflow question (perhaps upvote their answers if they are helpful). How to make a phone call using intent in Android?.
From memory, ACTION_DIAL will bring up the dialler and pre-populate the number.. it requires no special permission because it requires the user to actually place the call.
On the other hand, ACTION_CALL will actually initiate the call and requires special permissions.
If returning focus (bringing your app back to the foreground) is a concern, you could try using a scheduled intent to (re) launch your activity (maybe using alarm manager or similar) some time after invoking the dialler. You can retain state with a little care around launch modes in the intent and/or a special "do nothing" activity published in your app manifest which does nothing but re-activate your app.
I have created an accessibility service for my app where it would detect of the current outgoing call is answered or not but now i want create a callback function to my activity when the call is answered.
I have found some code to make callbacks from service in below links -
Callback from Service
RESTFul API
but i cant figure out a way to do that with my accessibility service, as i am not starting the service, android is, so there is no way to pass an object to the service.
If anyone can help please share.
Your service and your activity are separate processes. They cannot communicate directly. You should look into BroadcastReceiver. Note, though, that the user would have had to start your activity and have it running in the background. If the system decides it is low on memory and kills your activity completely, there is nothing you can do. You will not reliably get this broadcast with your activity in the background. You could try starting a background process from your activity for the sake of finding this event and storing the results somewhere for future consumption. Depends of course on your use case.
I need a way to log what app is currently running in the foreground, preferably without requiring root access or using the NDK. Is there any way to do this?
If I remember correctly, onResume() gets triggered any time an activity gets on foreground. If your app contains more than one activity, you should check all of them.
You can check activities lifecycle here
I am having trouble clearing my app's activity stack. At the start of my app I make the user login and give them a session id. After they login they are able to continue on using the app. However if there session expires I want to redirect them to the login activity and clear the Activity history so they can't have access to the app. I looked at the Android API and the Intent flag FLAG_ACTIVITY_CLEAR_TASK seems to be want I want but it was just included in API level 11 and no phones have the new OS yet. Does anyone have a solution to this problem. Thanks.
I found my answer here. Turns out that I have to broadcast an intent to tell all of the Activities to call the method finish().
The documentation for FLAG_ACTIVITY_CLEAR_TOP describes the situation you want if you use it in conjunction with FLAG_ACTIVITY_NEW_TASK
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
"This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager."
Basically I want my app to start running in the background when the ACTION_MEDIA_BUTTON is clicked. I've seen other apps do this, so it must be possible. I just don't know how it is possible. Some kind of special manifest code?
Currently my app can receive intents from the ACTION_MEDIA_BUTTON when it has been opened (and even running in the background) via intentfilter that I register in Java code of the main activity (when the app is first opened). But how would I have this button just... open up the app?
For reference, the following app can already do this functionality: https://market.android.com/details?id=com.kober.headset
Much appreciated, thanks.
Use a BroadcastReceiver and register for the intent. See this Android Developers Blog post for more details.