How to handle incoming intents from external applications in Flutter - android

Some services vary between Android and iPhone, for example floating widget, widget on the home screen, can I make them using flutter
Can I view a dialog on the home screen like this example?
update
thank everyone answer, but I need how to get it in Flutter

Yes you can show that
new AlertDialog.Builder(YourActivity.this)
.setTitle("Alert")
.setMessage("Alert Message")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Here you can do whatever you want to do on click
}
}).show();

Sorry you can't do it in iOS, however in Android, you can, all you need to do is write platform specific code.
You could create an Activity with the Theme.Dialog theme. In your AndroidManifest.xml file add the theme to the activity, like this:
<activity android:name=".DialogActivity" android:theme="#android:style/Theme.Dialog"></activity>
From your service simply start this Activity. You will have to start the activity with the Intent.FLAG_ACTIVITY_NEW_TASK flag. See How to start an Activity from a Service
Source

Related

Showing AlertDialog even when the application crashes

I have and android application in which I am initializing a third party library in the onCreate() of the main activity. Now when there is any runtime error in initialization of the library, My application will crash and at that point I want to show an AlertDialogue to the user saying "some error message" in the dialogue box.
The problem is that when the application crashes, my alert dialogue is showing up but it is dismissed along with the application. I want it to be persisted even when the application crashes.
Below is the code of alert dialogue which I used.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Can anyone guide me in how to make that AlertDialogue independent of the activity, so that it doesn't require the app window. If not possible, then what other widgets, I can use or customize?
If you want your app to keep running, avoid crashing.
If you want special handling for issues in a library init, catch the relevant exceptions and keep the app running in a state that shows the dialog and then does something else, such as finish()ing the activity that had a problem in its init.
To avoid the 3rd party library from taking down your app, wrap its API calls in a try/catch block, e.g.:
try
{
BuggyLibrary.init(); // example
}
catch (Exception e)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
You'll need to consider the other implications of this library not initializing ... if your app can't continue functioning without it you'll need to gracefully finish() your Activity when the dialog is dismissed.
May be it's possible to achieve something like that.
There is a library that allows you to show activity when your app crashes. Basically it launches new activity in another process.
At the same time you can show activity as a dialog by using Theme.AppCompat.Dialog for your activity as a theme.
Perhaps combining these two approaches will get what you want.
P.S. Or by using old fashioned try-catch as some people suggest.

Create notification when application is in background, Android

Initial description:
My application handles some events and do some task related to that event, even if application is in background. I am using receivers.
Problem description
I want to show notification when an event starts or end.
What I did yet
I used Notification manager :- But I need to show notification on home screen not on notification bar.
I used App widget :- But user will not able to notice any changes in information on app widget easily, because this does not do any eye catching effect.
I used Toast :- But this having time limitation to show, I want to show notification until user close.
So what should be the option? How can I do my task?
Please refer below image, as what I want
Unfortunately, you've explored all the possible options. What you want to achieve is not provided intentionally because it will make for very bad user experience. Imagine a notification (of the kind that you want) popping up when you are looking at an email!
Attention piracy
When your app is in the foreground, you can do a lot of things.
When your app is not in the foreground, there are only two options
Start an activity: You must do this only if essential (like an incoming phone call activity). If the user deems it more irritating than useful, your app is going down the drain!
Use the notification manager.
An option is to use Alert Dialogs
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
Or you can use run an Activity with the Dialog theme.

How to finish an android application?

I need to finish an android application. For that i wrote
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure You want to exit")
.setCancelable(false)
.setPositiveButton("YES"),
new DialogInterface.OnClickListener() {
// On
// clicking
// "Yes"
// button
public void onClick(DialogInterface dialog,int id) {
System.out.println(" onClick ");
closeApplication(); // Close Application method called
}
})
.setNegativeButton("NO"),
new DialogInterface.OnClickListener() {
// On
// clicking
// "No"
// button
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void closeApplication() {
System.out.println("closeApplication ");
this.finish();
}
}
But if any activity is not finished in the application, when i tried to exit the application that activity is finished first and the application is not exiting.. i tried 2 times to exit this application... How i can finish all the activities in an application when i need to exit... or is there any way to finish the entire application
To close application just call:
android.os.Process.killProcess(android.os.Process.myPid());
Otherwise due-to specific life-cycling of Android activities you can't be sure that application is closed/killed.
whenever you are starting a new activity use
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myintent);
and in manifest file mention that activity as
<activity android:name=".<Activity Name>" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Put this into your onClick or in onBackPressed:
moveTaskToBack(true);
finish()
Please read first this post from Google Android Developer Advocate Reto Meier :
When to Include an Exit Button in Android Apps (Hint: Never)
What is th symptom that make you want to add an exit button ? If you need to clear the activity stack and always restart with a specific Activity, maybe you just have to tweak your activity manifest declaration with attributes like : android:clearTaskOnLaunch
Android is made in such a way that virtually NO application that was once opened, is closed.
Before mis-interpreting the statement, understand this.
"Whenever you exit your app, Android saves all the things the app was doing (called its state) and pushes the app in the background, calling the onStop() method. this is the new state of the application then, where the app isn't running, but isn't flushed out of the memory too. whenever you start the app again, it is resumed from the frozen state. Only when the memory, where frozen apps are kept, starts getting full, the Android GC flushes the app."
So conceptually, nothing goes out. when you hit "back" button while ur on the first activity, Android bundles the app and data, and freezes it.
according to this answer,
just write this.finishAffinity(); and done!
I have an application with several Activities. I extended my Application class, and included a variable numActive. This is initialized to 0. Within each activity's onStart(), numActive is incremented, and in onStop() it is decremented. If the count reaches zero, the user has left my application entirely, and I close down my background tasks.
Shameless copy of NeTeInStEiN's answer as I found it so useful (please up-vote his answer): Sending a running application to background programmatically
You can use either:
boolean sentAppToBackground = moveTaskToBack(true);
if(!sentAppToBackground){
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
}
More information here: http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)
Or simply:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
According to Romain Guy a Android Framework Engineer, "You cannot simulate a press on the Home key.". So beware...
Check: http://osdir.com/ml/Android-Developers/2010-03/msg01887.html
Updated this answer according to: moveTaskToBack(true) returns false always
To Close the Application, you can also take "System.exit(0)" 0 is standard or use any exit code.

How to make an activity window stay always on top

I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities.
How can I do this on Android? Thanks
You can use the following code in the overridden onStop method of your activity:
#Override
protected void onStop(){
super.onStop();
Intent intent = new Intent(this, ClassNameOfYourActivity.class);
startActivity(intent);
}
Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.
And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.
So you won't be popular if you share it with Play :)
Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.
Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:
Creating a system overlay window (always on top)
How to create always-top fullscreen overlay activity in Android
You can't. As this is defined in the Android system.
Follow the steps to achieve your requirement
Create an activity which is going to be the top activity
Add the following code in the manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Use the following code to get overlay permission from user
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
new AlertDialog.Builder(this)
.setTitle("Permission Request")
.setMessage("This app needs your permission to overlay the system apps")
.setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivityForResult(myIntent, 101);
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
}

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