I wish to use service in background of my application. When some special place is reached inside service I wish to stop it and display to user notification, which wont disapear until he or she clicks "OK" (some kind of alert window). When he or she clicks "OK" -> I wish some data reached in service be passed to my Activity.
Can I please for help. I have my service already running well, I wish to use Alert Dialog - but have no idea how to invoke it from service.
As the official documentation states, A background service should never launch an activity on its own in order to receive user interaction.
Instead you should fire a notification to the notification bar. The notification can be flagged insistent and vibrate until the user takes action, which is as strong as not dismissing a popup until the user clicks a button. Follow the link above for good (and official) tutorial on notifications
If you really want to launch an activity (and pop up a dialog) you must set the following flag to your intent first otherwise you'll get an exception.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Once a certain point in your service is reached start an activity and dont use setContent view, but just use this in onCreate() to show a alert dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("ALERT")
.setCancelable(false)
.setPositiveButton("Ok", 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();
When the user presses okay, the dialog will close with the activity.
EDIT:
For the data you want to send to the activity just put it in the bundle starting the Activity.
Intent intent = new Intent(this, SecondActivity.class);
Bundle b = new Bundle();
// see Bundle.putInt, etc.
// Bundle.putSerializable for full Objects (careful there)
b.putXXXXX("key", ITEM);
intent.putExtras(b);
startActivity(intent);
// -- later, in Activity
Bundle b = this.getIntent().getExtras();
int i = b.getInt("key");
Related
I have this code to take you to other applications from my page.
Button more_apps = (Button)findViewById(R.id.more_apps);
more_apps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent more_apps = new Intent();
more_apps.setAction(Intent.ACTION_VIEW);
more_apps.addCategory(Intent.CATEGORY_BROWSABLE);
more_apps.setData(Uri.parse("https://play.google.com/store/apps/dev?id=****"));
startActivity(more_apps);
}
});
No problem other than that sometimes google play rejects the app and sends me a picture indicating that the 'more apps' button is promoting other apps. ..Bla bla bla
So I had an idea: when you click on the button, a confirmation message appears to go “yes” or “no”, and if you press “yes” it directs you to the link, and pressing “no” remains in the application.
How can this be achieved please
Probably the simplest way to show a dialog is to build using the AlertDialog.Builder().
You can use the following method from some random Util Class and just pass the arguments:
public static AlertDialog createAlert(String message, String positiveButtonText, DialogInterface.OnClickListener positiveListener, String negativeButtonText, DialogInterface.OnClickListener negativeListener, Context context, boolean isCancelable) {
return new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(positiveButtonText, positiveListener)
.setNegativeButton(negativeButtonText, negativeListener)
.setCancelable(isCancelable)
.create();
}
And you use it like this:
AlertDialog goToPlayStoreDialog = RandomUtilClass.createAlert(
"Do you want to see more of my apps?",
"Yes", positiveListener,
"No", negativeListener,
context, false);
goToPlayStoreDialog.show();
And here is how a listener is implemented:
DialogInterface.OnClickListener positiveListener = (dialog, which) -> {
**Start intent here**
};
As for the negative listener, simply call dialog.dismiss();
Note that you should implement the listeners before creating the dialog :)
I want to open a small popup or something like that on a button click such that the popup contains two buttons which on clicking goes to their corresponding activities.
Please provide the code as i am totally new to android?
Yes you can do this with Dialogs
use Below Code to make dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Uncomment the below code to Set the message and title from the strings.xml file
//builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
find details of dialogs in below link:
Dialogs
You can use material alert dialog for this
Check this!
I have an Android app, I need to add to it an AlertDialog that shows when the app starts. Also, I need to add to the AlertDialog a button (like: visit website), when the user click that button, it will open the link and browse it in the browser or anything else.
How I can do that?!
Thanks in advance.
Use an AlertDialog.Builder in your activities onCreate() method. You can use setPositiveButton() and launch your website in an intent, or webview when the user clicks it.
For example
new AlertDialog.Builder(mContext)
.setMessage("Launch Website")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Hide the dialog
dialog.dismiss();
// Launch the website
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(intent);
}
})
.show();
See http://developer.android.com/reference/android/app/AlertDialog.Builder.html
First you have to add an AlertDialog to your onCreate() method in your Activity.
After then You have to add a button to that AlertDialog.
There are three types of button in AlertDialog.
Positive
Negative
Neutral
Use any one of them.
Then when the button clicked you need to go to the website url, in some browser in the android device.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
Try this:
final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click to visit website!")
.setCancelable(false)
.setPositiveButton("Go to web site",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
}
}));
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
I am working on a project containing google maps. When the activity loads I want to check whether GPS is enabled or not. So I used the following code to redirect to the page containing settings.
if(!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ))
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Goto Settings Page To Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
But the problem is that if I enable GPS and come back to the app I want the page to load again so that the map loads. How do I ensure that my activity runs again?
You have to override the methods onResume.
If you know the activity lifecycle, you see that your activity is paused (onPause) when you go to activate your GPS, and onResume will be called when you return to your activity.
Put android:noHistory="true" in the manifest file of the present class.It will not leave any traces. Cleanup the stack.
i would like to have my rate button in my dialog to launch marketplace and go to my specific app.
Also how do i add in a message body into this dialog?
private void makeDialog() {
AlertDialog.Builder about = new AlertDialog.Builder(this);
about.setMessage("About The Giveaway");
about.setPositiveButton("Rate", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//action
}
});
about.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {}
});
about.show();
}
}
I wrote a simple library to do that.
It is called AppRate and you can find it on GitHub here.
Features:
Do not prompt the user if the app has crashed once.
Decide exaclty when to prompt the user. (number of launches ...)
Customize the rate dialog to fit your application design.
Usage example:
It is very easy to install and use:
Drop the jar in your libs folder.
Then include the following code in the onCreate method of your MAIN activity.
new AppRate(this)
.setShowIfAppHasCrashed(false)
.setMinDaysUntilPrompt(0)
.setMinLaunchesUntilPrompt(20)
.init();
This code will show a default rate dialog after 20 lauches.
It will be shown only if the app has never crashed.
The rate button points to your application in the Google Play Store.
I hope this can help you. :)
You can launch the Market app using an Intent. Add this to your positiveButton onClick (replacing the URL with your app url)
Intent browserIntent = new Intent(
"android.intent.action.VIEW",
Uri.parse("https://market.android.com/details?id=com.animoca.prettyPetSalon");
startActivity(browserIntent);