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

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);

Related

Display Alertbox from service

Is there any way to use alertbox in service rather then making another activity for it and then starting that activity from service?
A Service cannot show a Dialog , only you can show short lived Toast from there.
But If your Requirement is of showing dialog I will not disappoint you .You have got following options:
If you have a MainActivity use a LocalBroadcastReceiver to send a broadcast to your activity and let your MainActivity show a Dialog. (This will be possible only when your activity is visible)
The other option is Define a Dialog theme activity (take a look here) in your manifest and start that activity from service.
Another option could be , Define a Activitiy in Manifest whose sole purpose would be display a Dialog and start that activity from service
Edit
I have a suggestion , If you wish to present information to your users , you should use Notification . The main reason that you cannot show a dialog from service is that Google wants information to be decently presented to its users , instead of surprising them when suppose they are in middle of a call , or typing message etc.
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.
Alternative Solution
However If you persist I have another solution if you like hacking, (but I don't like hacks in good application).
Create a custom Broadcast Receiver
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// AlertDialogue will be here
}
}
Register it in the manifest file
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="com.sample.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
Trigger the BroadcastReceiver from service
Intent intent = new Intent("MyCustomIntent");
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("com.sample.A_CUSTOM_INTENT");
sendBroadcast(intent);

Loading my transparent activity hides the keyboard

I manage some events in a background BroadcastReceiver. In some specific situations I need to launch an activity. This activity is supposed to be hidden. Nothing should be displayed to the user.
It works well but if the user is using the keyboard, typing something, the keyboard is hidden. If the user is using GMail, writing an email, then a "draft" is saved.
It seems like this activity gets to the top and stops the one the user is using even if it is invisible. Is there any way to solve this?
This how I declare the activity in manifest:
<activity
android:name=".MyInvisbleActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
</activity>
This is how I launch it:
Intent intent = new Intent(MyContext, MyInvisbleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyContext.startActivity(intent);
This is the MyInvisbleActivity onCreate():
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(---); //This is not used
Window oWindow = getWindow();
oWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
--do work--
}
Thank you!
Android provides a theme for this android:theme="#android:style/Theme.NoDisplay"
Alternatively it sounds like you might actually want a service. But youd need to provide more information on what you are trying to achieve

native calling screen animation stops when dialog activity display on screen

I have on dialog activity with 2 buttons which is being display when native caller id screen displays. Two buttons are click-able and also I can pick up and reject call by android native screen buttons.
Problem is when this dialog displays, animation of native call screen stops. I have seen this things in Truecaller app and also Current Caller Id app. I have also used following some codes
Manifest.xml
android:launchMode="singleInstance"
android:taskAffinity=""
android:theme="#android:style/Theme.Dialog"
android:windowAnimationStyle="#android:style/Animation.Translucent"
android:windowBackground="#android:color/transparent"
android:windowIsTranslucent="true"
And activity.java
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
getWindow().setFlags(LayoutParams.FLAG_NOT_FOCUSABLE,
LayoutParams.FLAG_NOT_FOCUSABLE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
requestWindowFeature(Window.FEATURE_NO_TITLE);
and adding flags from intent call
Intent i1 = new Intent(this, activity.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i1.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
i1.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
i1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
I have tried lots of things to achieve this, How can I create dialog so caller id animation don't stop. Thanks in advance.
I implemented a solution that works well for me (and does not stop animation), here it is: https://github.com/inez/CustomIncomingCallScreen

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.

Android Services, Activities and Handlers?

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.

Categories

Resources