What is the way to create a popup screen that's opened from a background service while app is not running in foreground?
Refer to the Skype answer-reject dialog when there's an incoming call:
I have a background service that always runs even the app is not visible to user, but how can I pop up a screen like in the image from that service?
Update:
I am looking for the term used for this kind of screens, is it a regular activity started from the service, or some form of "notification" ?
This popup shows exactly that.
You can start an Activity with:
android:theme="#android:style/Theme.Dialog"
Do Read: If I can remember properly, I have read somehwere that it is against the Android guidline of using popup from a service. It recommends using a Notification service.
Related
We can't start activity from Work Manager according to the new Restrictions If App doesn't meet the requirements. All i want is to start activity from Work Manager even application is killed or no longer visible after some specific time. I do not want to run the App in foreground. Is there anyway, I can achieve this?
Any help would be appreciated.
If you have in house App or for testing, You can use this approach.
Add this permission in Menifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Also go to the App info settings
Setting -> Advanced -> Display over other apps
Allow this permission for this app. Now run the app, This will be able to start the activity from background.
But, I recommend to show notification and open the screen on tap notification.
You shouldn't start activities from background since it interrupts user's work.
If you want to open your app from work manager, you'll have to use notifications instead and set the notification tap action to open your specific activity using pending intents.
Here is a guide on how to create a notification in android.
Additionally, if your app needs to do something urgently, you can create a high priority notification, details here
i just want to know how can i detect if the user opens an app so an activity of mine launches as well.
For example, the user opens the sms app and right after a kind of lockscreen appears.
You can create a service which will run int the background and you can use this API to determine which activity is visible. That's how many app lock works.
As far as I understand the Android system, it is not possible unless you are making a custom firmware.
I'm working now in a project to download images from the web and then show it after completing its download.
i made an activity class that has a button and text field and when the user press on the button, i started a service which will show a notification in status bar and after download i will show a message .
my main problem now is i want to go to other application or home page while the download is working.
i tried it but i after the download i got the message ,,, " Force quit"
should i use broadcast receiver or remote server to handle what i want.
Thanks,
The service should be the one downloading the content. If you go to the home activity our activity calls onPause, stoping all the work in progress.
Activities are just that, activities, they only work when visible and are built strictly for user interaction or a user activity, so this is impossible. Use a service instead. Here is a diagram showing lifespan of an activity:
Link to App activity explanation on Android developer site
You may need to call Looper.Loop() in your service to keep it running if you have any listeners in your service.
I am successfully able to start my Android app automatically on boot using BroadcastReceiver with an intent-filter BOOT_COMPLETED. In my onReceive method, I start the launcher activity for my application.
However, I don't want this application to be in the foreground on boot, but I do want it to be on the activity stack. Is there a way to still have the home screen show up on boot, but also have my application starts up. (I don't think I want to use a Service, because my application has UI.)
Decision on weather you should or should not use Service in appliction is independent on weather it has UI or not. All "third party" apps have UI, there is little use of application without at least 1 activity.
So in your case, just use Service.
I want to write an app to send an sms from a shortcut on a home screen. That is all.
I just can't understand within the framework how I can write such an app. Here's what I've tried so far and what my ideas are:
I wrote an activity that sends an sms using SmsManager within the onCreate() however, this just keeps on sending messages even though the code is not in a loop. I realise I must be not be using an activity the way it's designed to be used. The android application fundamentals article says an activity is for displaying a screen or gui, but I don't need a gui. I just don't know what component I need to to use.
A service? no, because I don't need something running forever in the background.
An activity? I guess no because I don't need a gui.
I had an idea to create a broadcast receiver which would respond to a broadcast, so my sens smsm code would be in there ready to send when it receives the signal. But how do i send the signal from an app shortcut on the home screen? What would be the entry point of the app.
I'm just really confused, I've read the tutorials and the app fundamentals and searched forums and not found the answer. There's is just a big gap in my knowledge of the android framework that needs filling I guess, once it clicks I'll be fine but I'm just stuck right now.
Thanks people.
Service does not have to run forever. You can control how long it works in the background, you can even create Service that will shoot once and disappear. Suggestion:
from your shortcut (app icon) start Activity. That will be activity with translucent background. To achieve that skip line setContentView() and define theme
#android:style/Theme.Translucent
in your AndroidManifest.xml. This way you will avoid black screen flash at Activity startup.
from that Activity start Service and call finish() on that Activity
perform SMS sending (you already know how) from Service. Maybe, you do not even need Service, you can send SMS from the translucent Activity.
call stopSelf() from your Service immediatelly or after some short timeout (wait for SMS sending result).
All described can be done smoothly through Widget framework. In that case you can even have custom button user may press on. So, that would be another approach.