How to show dialog in activity from service? - android

I'm developing an app that downloads some data in service and show it via push-notifications, also user can read other data in activity. So could you explain me how can i make a dilalog that's show's to user when WIFI/3G is turned off and says something like this "For normally app working you should turn 3G/WIFI on". A problem is that i must implemet it by service callback, i.e. by this logic:
service start downloading data --> No internet --> Cheks if WiFi/3G is turned off --> Shows in any Activity a Dialog.
I know that i can make it by using a receiver, but how a can call a dialog in every activity? Or i don't need an activity, just a context in OnReceive method, right? Please give me any ideas.

Or i don't need an activity, just a context in OnReceive method, right?
No, that is incorrect. A Dialog needs an Activity. You could show a Toast from onReceive() using the Context that it has.
Or, as Junior was suggesting, you can create an Activity with a Dialog theme. To do this, you simply need to add the following line to the <activity> tag in your manifest.xml for the appropriate Activity
android:theme="#android:style/Theme.Dialog"
This is nice because it gives you all the functionality of an Activity while displaying as a Dialog so the user doesn't feel like they have "left" where they are.

Launch an activity (of your own) with a dialog theme.
Hope it helps!

Related

On Launch Icon Click Event

Can We write an event for clicking App Icon. I need to display a string during the Application icon click. Can anyone help on this. Thank you.
Firstly the question is where you want to display your string. As you didn't mention developing a widget or anything else but an Android app, my guess is you want to display it in your app.
So the closest convenient callback to the start of your app should be the onCreate method of your Application class. For this, you will need to extend the application class. You can see how to do it here:
https://medium.com/#balakrishnanpt/android-application-class-a8a1d64c82d1
At this point, you will be limited to showing a Toast message as you don't have any Activity yet.
As an alternative, you can create your first launcher activity and display your text in it's UI from the activity's onCreate method.

Change an application's default activity programatically without AndroidManifest?

I have an application wherein when the user first open's the application he is asked to login with his credentials. So this activity say "Register" is the default activity. But once the user has logged in I want the default activity to change to some other activity say "MainActivity" from there on.
So how can i achieve this programatically?
I know one approach is to create a blank activity and based on a flag to launch the desired activity. Is there any other approach other than this ? And is the previous approach efficient ?
Thanks in advance :)
it is efficient, how ever you dont have to create a new blank activitiy, rather just use your mainActivity and if the user is not registered spawn off the Register activity cause that activity is only going to happen once. and the normal usecase is to stay in main activity.
that is how most people do it:) hope this helps

Launching dialog activity from services caused 'MainActivity' to also launch

I am designing an app that is used for emergency alerts. The alerts come from a server and a connection to that server is maintained in service.
If the service receives an emergency request from the server it checks to see if a specific activity is open. If it is it lets it know an emergency has been triggered and the activity launches a dialog activity with some options. It then handles results from this new dialog activity.
However, if the service notes that the activity is NOT open I want it to launch the dialog anyway. I know that this isn't good practise but because of the importance of this emergency I don't want to rely on Notifications (which are already in use if the activity is closed to let the user know that the app is still listening for emergencies).
What currently happens is that the below code is executed in the service and the dialog launches. However, the 'main' activity (the only other activity in the app) also opens behind the dialog. What I really want to happen is that either...
1) The service launches the main activity which then opens the dialog so that I can easily capture the results.
2) The service launches only the dialog activity and I use a broadcast receiver to capture results from this activity.
1 would use the mechanics that already exist for capturing results from an activity. However I don't like the idea of chaining the activities together in this way.
2 means I can ignore the main activity all together (because I don't really need it in this instance) but seems more of a get around.
What I am really asking is two things. What is best practise given my circumstances and how do i achieve number 2? Here is the launch code in my service. Notification in this code is referring to the dialog activity that will open.
if (MainActivity.isActivityInUI) {
//Dealt with by activity
sendMessageAlert(message);
} else {
//Launch dialog directly from service
Intent notification = new Intent(this,
EmergencyNotificationActivity.class);
Bundle args = new Bundle();
args.putString(MobileMessage.EXTRA_LOCATION_NAME,
message.locationName);
args.putString(MobileMessage.EXTRA_ID,
String.valueOf(message.id));
args.putDouble(MobileMessage.EXTRA_LATITUDE,
Double.valueOf(message.latitude));
args.putDouble(MobileMessage.EXTRA_LONGITUDE,
Double.valueOf(message.longitude));
//and the flag to let the notification know this is from a service...
args.putBoolean(EXTRA_FROM_SERVICE, true);
notification.putExtras(args);
//add flag because this is being called from outside of an activity
notification.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |ActivityInfo.LAUNCH_SINGLE_INSTANCE);
startActivity(notification);
I think instead of trying to show a dialog without visibly showing an Activity, you should consider launching an Activity that is themed like a Dialog. Just apply the following theme: http://developer.android.com/reference/android/R.style.html#Theme_Dialog (or similar themes) to your EmergencyNotificationActivity. You probably would have to tweak your class to behave like a dialog instead of launching one (which I am assuming is what you're doing currently).
This method would also allow you to not have to check if an Activity already exists.

Launching Dialog from Service

I want to launch a dialog from a service that hovers over whatever the user is currently looking at. The dialog gets launched like this: service gets trigger to open dialog > start transparent activity > transparent activity shows dialog.
My problem is when the user opens the app, launches into the main menu, and then presses HOME to leave. By pressing HOME, it leaves the main menu activity on pause, not destroyed, and when the service starts the dialog, the main menu gets shown underneath the transparent activity; causing the dialog to loose the affect of hovering over whatever the user is looking at.
How can make it so that the transparent activity gets opened independently of any other activities in the app? The only way to prevent this currently is to finish all the activities when they are paused; but this is impractical.
This is the last thing we want. :-)
1. Dialog boxes from Services
One of the best experiences in mobile devices, IMHO, and Android in particular, is that after decades, we got rid of system-wide, pesky alert dialogs. Finally, best practices [1, 2] for user interaction gave us a way to avoid the infamous disseminate use of MessageBox(hwnd, lpText, lpCaption, uType), competing for focus and for the attention of the poor user. See video parody above.
The reason it feels awkward to start a dialog from a Service is exactly because it is supposed to be a background task, without user interaction. By concept, you shouldn't be doing this. That's the reason why we see these tricks (transparent activities, what a silly thing) to cheat the design guidelines in the first place. They are bad, they disrupt the user experience, they steal focus and attention. They disrupt our work.
2. Use notifications instead
Whenever you want to notify a user of something from the background, when the user is somewhere else, you use a notification. It's the default pattern, and it doesn't bother the user.
Therefore, you should be sending notifications from your Service.
From there, if the user is interested, then he will touch the notification and you start your own activity, possibly resuming your activity, creating a new one, and then using a dialog requesting action to be performed, or whatever you want to do.
3. Finally, do NOT use FLAG_ACTIVITY_MULTIPLE_TASK
You should not, ever, use this flag unless you have carefully read and fully understood the documentation, and the implications of using that flag.
Do not use this flag unless you are implementing your own top-level application launcher.
(...)
Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.
Really. In this case, just don't.
You may consider using alertDialog with TYPE_SYSTEM_ALERT instead of activity:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
Please note that you have to use the following permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Found this out myself, just add the Intent.FLAG_ACTIVITY_MULTIPLE_TASK flag to the launch Intent; of course in conjunction with the Intent.FLAG_ACTIVITY_NEW_TASK flag.

Start service from main application icon

My application is basically a service, i.e. has no UI (there isn't any class subclassing Activity). I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
Is this possible?
Found the solution to this:
Create an Activity and in the onCreate(...) method call your service and then call finish().
To prevent the flashing of the Activity opening and closing, use this as the theme of your Activity (in the AndroidManifest.xml):
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Just make one Activity that allows the user to configure your application. Also there surely must be some sort of UI as part of the app. Otherwise what is it actually doing and how does the user control it?
I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
That is not a "list of applications". It is a list of activities that have advertised via <intent-filter> that they are to appear in the launcher. The key word is "activities".
Creating a dummy Activity that starts the service and then finishes itself does the trick, but I'm a little worried about the flashing of the Activity loading and closing.
Then create an activity that adds value to the user.

Categories

Resources