I have an activity which can be launched from two sources. Each source launches it's own instance of the activity:
1. From inside an application
2. From an external service
My problem is with the service. The service needs to be able to control the visibility of the activity on the Android screen (hide and show the activity).
Currently I start the activity from the service with the following intent:
Intent appIntent = new Intent("Custom_Action");
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(appIntent);
My question is, how can I control the visibility of the activity. Maybe through binding the activity to the service?
I think you're misunderstanding how services and activities relate. A service runs in the background, doing it's thing. It has no UI and no way for the user to interact with it. The activity can send the service messages and query its state but the service has no way to push information to an activity. If you want the service to notify the user of something you can post a notification. Then have that notification launch your activity which will then connect to the service. There is no way to force open an activity from a service because there is no way to know if your user is even looking at their device at that time.
Related
I am working on an Android fall detection application. When user falls, alarm with timer turns on and if user not clik cancel within 15s, app send SMS to contact. Everything works fine when app is open but I don't know how my foreground service should work. Is it possible to make foreground service work like that- after fall detected foreground service open application and run timer from Main Activity?
Code from foreground service opening MainActivity:
Intent myIntent= new Intent(this, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
There are two obstacles to your use case:
Firstly, starting from Android 10, there are new restrictions on starting an activity when your app is in the background (and as it's written on the android developer page, even with a foreground service, your app will be considered in the background if no activities are in foreground). So you won't be able to "pop" a view to the user, except if you target a version < 29 (but it's not very recommended).
Secondly, since the beginning of 2019, the Google Play Developer Policy has been updated and you cannot use anymore the SEND_SMS permission, unless if you can justify in the publish console that your app is an SMS handler and you will need the user to register it as the default one. So you won't be able to send an automatic text message directly.
You can try to change the notification message to alert the user and try to have him click on the notification to open an activity.
I am trying to create an app that will keep my device wake until I tell it to turn off via code. I can use the follow code in a application...
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
That will work until I tap on the home soft button and then the flags will clear. So, I thought of putting that code in a foreground notification service and broadcast receiver for my action buttons. I can't getWindow in a service or broadcast receiver classes unfortunately. Is there an easy way to get around this?
My goal is to setFlags to keep the screen on from the Activity, and then the service takes over from there so I can close the app, and the keep on screen stays on until the service is turned off.
I've decided to use a service for the Android Studio app I'm working on; however, I can't seem to find a way to reference the service in my project. The service will enable the mic to continuously record and using a thread, notify the user if the sound is over a certain loudness and then update the phone's location accordingly. So within the service, if the mic listens into something above a certain loudness threshold it will pass the information to the Google Maps activity. The service is a started service that starts after a button in another activity is pressed.
Is there a way to reference the service, possibly a variable within the service, from my Google Maps activity java code? Do I have to use a bounded service instead?
You describing a service that should have connection to ui, notify it about it state and possibly allow changing it. You should you use bounded service for that, they have exactly what you need: https://developer.android.com/guide/components/bound-services
I have an app that when it launches, starts the main activity, all its okay, this activity sends and listen to info from an XMPP Server. If I change activity this send/read doesn't stop but when I go back to main activity, ** I reconnect to Server. Its okay but the Server read Disconnect-Connect when I go back to main activity from any other activity.
My question is, how I can launch the main activity from other without Disconnect-Connect issue (?)** I think maybe exists a method like startActivity but without restart It (I don't use finish)... Only go back to activity showing it, but never stop or start It... Something like show(enable/disable) . The activity works and the second plane and never stop/restart. This activity has a lot of threads that cant be rebooting all time.
Why dont you make you client-server communication in Application class, so you always have the same instance, and you will be able to manually control lifecycle of communication?
My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.
But the exiting of these blocked application did not work perfectly.
Is it possible to get close/exit event of an application?
How can i finish an application as whole(By finishing all its applications).
How to launch an application without having any history of previous launch?
Thanks in Advance
Is it possible to get close/exit event of an application?
Yes it is possible inside your LauncherActivity You can override onDestroy this method will be called on application exit.
How can i finish an application as whole(By finishing all its applications)?
I believe you want to stop your all running activities here. This can be achieved in multiple ways.
android.os.Process.killProcess(android.os.Process.myPid());
or
Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities and will brings the user to the HomeActivity. While calling this you can add a flag in intent and using that value you can finish the HomeActivity also. Use finish() method to finish the activity.
How to launch an application without having any history of previous
launch?
You can use the same above Solution to achieve this. The second one.
Hope this will help.
There is an onTerminate method in application class, but this cannot be used in production environment. See more about this here