Android Services, Activities and Handlers? - android

Im developping an Android Service in Android that needs to pop-up a new dialog box for confirmation.
I can popup a new activity using Intent and Context
Intent myIntent = new Intent(context, ConfirmationActivity.class);
But then I need to handle the option selected in the dialog box (OK or Cancel).
Any suggestion?
Note: Not developing for smartphones.
Update: I need to return the result to the place I call the Dialog.

A service should not pop up anything at all. Imagine the user is in the middle of a phone call when your dialog pops up.
If you need a confirmation from the user the best thing you can do is to use the NotificationManager and use a PendingIntent to launch an Activity. The Activity can still have a dialog style if you like.
Control flows back from the Activity to your Service then, so when the user presses ok or cancel you would either call a bound interface or use SharedPreferences to tell the service about the user's choice.

Related

How do I use Espresso with System dialogs

I have buttons in my app that launch Android Chooser intents (ACTION_VIEW, etc.) And i was wondering how to use an Espresso idling resource. to wait and detect display of the dialog and how to cancel it and return after.
I am not asking for code per se .. just wondering how to do it?
e.g.
onView(withId(R.id.ad_email_button)).perform(click());
this button opens the intent
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
So how to cancel the dialog so the test doesn't hang and verify the appropriate system dialog was shown?
As far as I know using Espresso we are limited to actions and verifications inside specific Activity. But I found out that we can access UiAutomator as well and perform BACK action to dismiss alert dialog:
InstrumentationRegistry.getInstrumentation().getUiAutomation()
.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
Looks like the answer is to use UIAutomator for these tests:
https://developer.android.com/tools/testing-support-library/index.html#features

android widget prompt for unlock

I've a problem developing an application, I'm building a Widget that can be either on lockscreen or in home page. when is in lockscreen I want that when the user clicks a button the widget prompt for the user login in case of needed. I mean prompt for patter or password or face recognition depending if the user have any of this security enabled.
after the user enter his pattern the widget will run an application (intent).
I've notice that some widgets does prompt for login and other doesn't I haven't found the difference between them.
I think this has to be on the onReceive method but not sure how can I call this "login" method, so far on the onReceive I start the intent
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.getApplicationContext().startActivity(i);
but this still not prompt for login. when the widget is on the lockscreen.
let say that the user interacts with the widget in the Lock screen, the user is not prompted to enter the password, either way the user does enter his pattern|password and after the user "enter" his home screen and the application is started. So the widget does start the intent just don't let the user "know" or login to see the application.
any thoughts on how can I prompt the user to enter his pattern so it enter to the home screen?
[Edit]
The problem occurs when I try to fire the intent within a BroadcastReceiver.
What I do is allow the user interact with the widgetd buttons, depending on what the user has selected I'll open my application adding some extras to the intent.
so on the onReceive method of my BroadcastReceiver I try to fire:
Intent i = new Intent(context, MyActivity.class);
i.putExtra(WidgetUtils.TAG_CURRENT_SELECTION, StringIGotFromInteraction);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);
this way the intent don't prompt for the "unblock".
If in the onUpdate I cast the pending intent to any button when the user clicks that button does prompt for the unlock this way:
views.setOnClickPendingIntent(R.id.aButton, buildAPendingIntent(context));
but I need to know what has the user selected to open and Intent in case I open any.
and my contex.startActivity Approach works fine when the widget is on home screen just not on loockscreen.
Any thoughts how can I make this happen.
I couldn't find the answer what I end up doing was create a dynamic button and when the widget is updated (by the other buttons) I assign the PendingIntent to this button.
So when user clicks this dynamic button the widget attempt to open the intent and is there when the user is prompted for the password.
I think this is not the best solution seance we make the user make an extra click on the widget.

Loading Dialog box in android

I am developing one application in Android. My application has one service. My service is always running in background. My background service keep monitoring of region define by user.
Now when user go out range my application give alert to user. Now my problem is below :
When my application is closed i.e. not running, then my application
get alert.
When my application is running then sometimes it get alert and some
times not.
Below is the code i.e. activity that will load alert for when user go out of range.
//SecuRemote.LOG("current activity context" + SecuRemote.currentActivityContext);
Intent i = new Intent(context, ShowAlertDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("Msg",msg);
if(status){
i.putExtra("status","true");
if(devName != null)
i.putExtra("devName",devName);
} else
i.putExtra("status","false");
SecuRemote.LOG("start activity showAlertDialog" + context);
context.startActivity(i);
Looking some good response...
Regards,
The problem might be in the programming logic -
Your service should probably be checking for a flag that says isInside or not,so by the time user enters your application this flag will be toggled saying isInside=true and onPause or on onDestroy whatever you think is gonna serve your purpose you should toggle the flag
//Service part and dialog showing
so in your service as it will be checking for the flag,as soon as it checks isInside=false it raises an alert using the code mentioned above....I would recommend you to show that above activity(the alert activity) as an dialog by putting the theme attribute of that activity as dialog ....
android:theme="#android:style/Theme.Dialog"
i hope it helps you.

Shown an alert from a non activity

In my app there is a polling task which calls a web service periodically. According to the response of this webservice I need to display an alert dialog with two buttons.
But I'm not able to display the dialog as an alert dialog can be showned only from an activity. Only toasts are able to display. Is there any methods to display the alerts?
Showing Alerts from a background process is not a desired functionality.
I would suggest you to display a Notification in the Notification bar.
You can start an Activity from your Background Task with
Intent myIntent = new Intent(getApplicationContext(), MyActivityNameGoesHere.class)
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(myIntent)
In this activity you can start a regular dialog.

Android - How to display a dialog over a native screen?

I was wondering if anyone can tell if how to pop a dialog screen up over a native Android screen?
I currently have an application that traps an outgoing call and stops it, I then want to pop up a dialog that would take over from the dialler screen and alert the user that there attempt to call has been blocked and allow them have some new options from the dialog.
I know that some people will say that I should use notifications instead but I'm aware of that and its not the way that it should work, I need to be able to pop up a dialog when the call gets trapped.
This is my dialog code so far
AlertDialog LDialog = new AlertDialog.Builder(context)
.setTitle("Call Blocked")
.setMessage("Call Blocked, reroute call?")
.setPositiveButton("ok", null).create();
LDialog.show();
I presume I have to somehow get the context to be that of the dialler screen?
Can anyone offer any help and assistance or links to tutorials?
Thanks in advance
For my application I used an activity with the Dialog theme.
You can declare the theme in the manifest file :
<activity android:name="PopupActivity"
android:launchMode="singleInstance" android:excludeFromRecents="true"
android:taskAffinity="" android:theme="#android:style/Theme.Dialog" />
use launcheMode="singleInstance" and taskAffinity="" if your popup is detached from your main application. Otherwise user may click the back button and return to the previous activity of your application.
excludeFromRecents="true" to avoid your popup to appear in recent tasks (long press home)
theme="#android:style/Theme.Dialog" to set the Dialog theme.
How to get the equivalent of launchMode = singleTask in code
I have not seen a clear explanation of how to set these flags programmatically, so I will include my results here. tldr: you have to set FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
If you launch this directly from your app, your dialog will appear on top of your app's last Activity. But if you use a PendingIntent broadcast by AlarmManager to launch your "dialog", you have time to switch to a different app so you can see that your "dialog" will appear over that other app, if the style is set appropriately to show what is behind it.
Obviously one should be responsible about when it is appropriate to display a dialog on top of other apps.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// you have to set these flags here where you receive the broadcast
// NOT in the code where you created your pendingIntent
Intent scheduledIntent = new Intent(context, AlertAlarmActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(scheduledIntent);

Categories

Resources