Checking if GPS is enabled in android - java

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.

Related

android studio exit confirmation dialogue not working

public void exit(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("Likee Likes");
builder.setMessage("Do you really wanna Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create();
builder.show();
}
**I am using this code to confirm my user either exit or not. when my user click the "yes" button the app doesn't close and get back to the previous activity. Is there any mistake with this code? **
i am trying to close my app by user confirmation.
I assumes you use AlertDialog in the another activity rather than your first activity, so when you use finish, you are close the activity that isn't the first activity.
If you you want to close you app, you can try use startActivityForResult to process some job accordingto the requestCode.
But now startActivityForResult is deprected, you can try to use new way to do this: OnActivityResult method is deprecated, what is the alternative?.
You can reference to this too How to quit android application programmatically

Add a link to a button on an AlertDialog that shows when the app starts

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

How do I make the AlertDialog box appear outside the app?

#Override
public void run() {
//Create thread that can alter the UI
AlarmPage.this.runOnUiThread(new Runnable() {
public void run() {
cal = Calendar.getInstance();
//See if current time matches set alarm time
if((cal.get(Calendar.HOUR_OF_DAY) == alarmTime.getCurrentHour())
&& (cal.get(Calendar.MINUTE) == alarmTime.getCurrentMinute())){
//If the sound is playing, stop it and rewind
if(sound.isPlaying()){
ShowDialog();
alarmTimer.cancel();
alarmTask.cancel();
alarmTask = new PlaySoundTask();
alarmTimer = new Timer();
alarmTimer.schedule(alarmTask, sound.getDuration(), sound.getDuration());
}
sound.start();
}
}
});
}
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT);
}
});
alertDialog.show();
}
I am making a simple alarm clock app that notifies the user. I want to make a alert box that gives the user the option to turn off the alarm when it goes off. I was able to make the alert box, but it only appears in the app not outside of the app. I understand the app has to be in the background running. If I need to show more code or be more specific, just ask please.
Add a line as:
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
// line you have to add
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
check now.
Do not accept answers if they don't address your question, it is misleading.
The accepted answer is not correct, as it will never work outside your application.
Reason:
It requires an activity context not application context.
If you provide application context, your app will crash with IllegalArgumentException- you need to use Theme.AppCompat or their decendents...
If you need functionality as actually stated in the question you have to have a separate activity themed as a Dialog like here
or you can add a custom view to your window using window manager and making it system level alert like here.
Do this create an Activity without ContentView or a View associated with it and call your alertDialog method in your onCreate also remember to set the background of the Activity to Transparent using ColourDrawable
And that activity will look like a dialog or will suit your preference, you can also fall back to Themes so you can set an Activity as Dialog and treat it like Dialog also use DialogFragment

android service and "alert window"

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

Make rate button launch marketplace and have message body show

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

Categories