android prompt user to enable location services intent error - android

I have an app that needs to use location services for one of its features. So when the user clicks on a button, I would like my app to check to see whether the device has location services enabled, and if it doesn't prompt the user to enable it via an AlertDialog. When the user clicks the 'Enable Location Services' button, I want the app to take the user to the settings page where the user can enable it.
My problem is that when the user clicks the 'Enable Location Services'
button, instead of opening up the settings page, the app goes straight
into the main activity.
Note: This is all happening inside of a fragment whose parent activity is my second Activity. So here is the layout of my app: MainActivity (login page) => Second Activity (parent activity) => fragment (where this check must occur).
Here is my code inside of the fragment's onCreateView method:
currentLoc.setOnClickListener(new View.OnClickListener() {
DataActivity dataActivity = (DataActivity) getActivity();
Context context = getActivity().getApplicationContext();
#Override
public void onClick(View view) {
if (!dataActivity.isLocationServiceEnabled()){
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("To use this feature, you must first enable location services");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Enable Location Services", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}});
alertDialog.show();
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("GPS Enabled...");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
});
Note: currentLoc is an imageView that is created inside the
fragment_layout file
More information:
compileSdkVersion 26
buildToolsVersion "25.0.3"
minSdkVersion 14
targetSdkVersion 26
compile 'com.android.support:appcompat-v7:26+'
compile 'com.android.support:support-v4:26+'
I have tried countless variations that all use intents and for this project they all take me back to the login page (MainActivity). When I create a new project and use the same code, it works...

Related

Android Toast text on next activity

I have an intent and I want a toast to show as soon as we get to that intent. Code:
Toast.makeText(this,"Please enable internet connection",Toast.LENGTH_LONG).show;
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
As you can see im going to the android setting, and I want it to prompt them to activate WiFi. Any ideas or better ways to do this?
I would rather recommend to show a dialog which asks the user to enable the wifi connection for him and then call WifiManager.setWifiEnabled() to switch on wifi.
This way, you don't have to leave your app. It is not possible to show an toast from an external activity.
Quick alternate approach:
Don't show a toast. Display a dialog saying "Please enable internet connection" and when the user presses the "OK" button fire the intent to send him to the settings page.
You could use a dialog, here's an example
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("WiFi Settings");
builder.setMessage("Please enable internet connection");
builder.setCancelable(false);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Launch settings activity
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)););
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
builder.create().show();

Android - Firebase calls duplicates Map

I am developing an application in Android Studio with Firebase where I am using the map to show different things. The first time the user enters the MapsActivity a alertdialog pops up with some information. My problem is that when the "OK" button is pressed the application automatically duplicates the activity and the duplicated activity is ran. When i press the back button I can go back to the "original" activity again. I think it is because of the multiple Firebase calls. Are there any fix to this?
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(m1)) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this);
dialog.setTitle("Bus stop");
dialog.setMessage("Do you want your kid to get picked up here?");
dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
database.child("Addresses").child(m1.getTitle()).child("name").setValue(m1.getTitle());
database.child("Addresses").child(m1.getTitle()).child("time").setValue(m1.getSnippet());
database.child("Addresses").child(m1.getTitle()).child("users").child(userId).setValue(username);
database.child("User").child(userId).child("address").setValue(m1.getTitle());
}
});
dialog.show();
}

Android: How to make AlertDialog disappear on clicking ok button?

I am asking the same question which is asked before at below links but the solution proposed in these links is not working for me so I am posting it again.
How to make an AlertDialog disappear?
Android AlertDialog always exits when I click on OK Button
How to navigate to next activity after user clicks on AlertDialog OK button?
Basically, I am creating an AlertDialog builder to notify the user for asking to enable a setting for the Usage Data Access and when the OK button is pressed then the Settings menu gets opened. When I press back button to come back on the app then the AlertDialog is still available there although I expected to be dismissed to be back on my app.
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
builder.show();
return;
}
Any hint what wrong could be going here?
Edit after some testing:
I tested OPs code on 6.0.1 and it behaved as expected - i.e. the dialog was dismissed after clicked 'OK'. I'll leave my initial answer below as an alternative that also works. Additional alternatives can be found here.
You can get a reference to your Alert Dialog it from your builder.show() method:
mMyDialog = builder.show();
In your onClick method:
mMyDialog.dismiss();
Full sample:
AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
mMyDialog.dismiss(); // dismiss AlertDialog
}
});
mMyDialog = builder.show(); // assign AlertDialog
return;
}

Video call got disconnect due to do not keep activities check in android device

ongoing video call got disconnected when it goes to on pause because of do not keep activities checked under developer option in android device how can a user stay in video instead of that option is checked
You can check "Is the do not keep activities is checked or not" by using below line,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
If it returns one, create a AlertDialog and ask the user to disable it first, if user clicks on positive button then create an intent for the Settings like below,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
if (value == 1)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("You need to disable `do not keep activities` option, press YES")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I am entirely not sure if I got you correctly.
But does this: https://developer.android.com/training/scheduling/wakelock.html
answer your question?
It prevents your app from entering onPause, when a voice-chat is ongoing.

android dialog wont close after opening system location settings

I have a dialog that prompts users to enable gps location if it's not enabled.
After opening the settings, and the user enableing gps, and pressing the back button from the location settings screen they come back to the app, but the dialog is still visible.
Here is the code for the button clicked.
// On pressing Settings button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
dialog.dismiss();
}
});
My question is why does the dialog.dismiss() not close the dialog, I have also tried dialog.cancel() with same result. Is there something I should be doing after opening the settings screen?
Thanks,
I have exactly this code in my Activity:
private void showLocationDisabledInfo() {
final Context c = this;
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setMessage("TEST");
builder.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
c.startActivity(intent);
}
});
builder.setNeutralButton(R.string.cancel, null);
builder.create().show();
}
and it does close the dialog automatically, no matter what button I click. It's from my application, and it was tested on devices with APIs 8 and 10, and emulator running API 17. The only difference (possibly) between our codes is the value of mContext. Please provide the whole code responsible for setting up the dialog and the environment in which you've seen described behavior.

Categories

Resources