I have a simple if statement check to determine if the GPS is turned on on my app.
This works perfectly on the emulator, but when installed on an actual device instead of the 'Location & security' menu being loaded as called from the intent, it is loading the AGPS option. This menu does not allow me to turn the GPS on and I have to manually naviagte to the 'Location & security' menu.
This is my alertDialog builder code. As shown using an intent to call 'ACTION_LOCATION_SOURCE_SETTINGS'.
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
To highlight the issue further ill use the following screenshots:
This is the screen I want to load through my intent:
The menu that is currently being loaded:
How do I enable my intent to load the first menu instead of the second?
Related
My main question is: Is there anyway I can get trigger an AlertDialog from inside onOptionsItemSelected() without it crashing my emulator when I press a button on the dialog?
I have looked all over the internet for this but everyone I find keeps saying the same thing but even when I copy and paste their code I get the same error. So I'll try explain my situation as well as I can.
Here is an example of a pretty simple AlertDialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
.setTitle("Your Title")
.setMessage("Click yes to exit!")
.setCancelable(false)
.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Now this WORKS. As long as I run it OUTSIDE of the onOptionsItemSelected() function. For example, if I add this piece of code inside the onClickListener for a regular Button. Then the alert dialog will appear when I click the button and everything will work. However when I include this piece of code inside my onOptionsItemSelected(). Then the alert dialog will appear, but pressing a button on the dialog will crash my entire emulator. Here is my onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_trash:
Log.i("trash", "button clicked");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
.setTitle("Your Title")
.setMessage("Click yes to exit!")
.setCancelable(false)
.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;
case R.id.action_help:
Log.i("help", "button clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now when I click on the item in my toolbar linked with the action_trash id. I get an alert dialog, but when I click the OK button, my entire android emulator just crashes. And the only warning I can still see is this:
Hax is enabled
Hax ram_size 0x40000000
HAX is working and emulator runs in fast virt mode.
emulator: Listening for console connections on port: 5554
emulator: Serial number of this emulator (for ADB): emulator-5554
EmuGL:WARNING: bad generic pointer 0x7fc16d378600
Which I am pretty sure is an unrelated message. I sometimes see people asking for LogCats, but there are none that I can find since the entire emulator has crashed.
And as a side question: why doesn't this even work at all? Is it because the onClickListener() made inside setNeutralButton() has somehow been destroyed? I am fairly new to android, so if this is some big nooby mistake that can be avoided in the future any advice would be appreciated.
P.S. I have also tried replacing '.Builder(this)' with '.Builder(MainActivity.this)' and all the variations I have encountered so far and none of them solve the issue.
Thanks in advance :)
I've ran into this same thing. Two separate things worked for me.
Changing the AVD settings to have "Software - GLES 2.0" instead of hardware or auto for the "Emulated Performance" option.
Turn on "Show Layout Bounds" in the developer options of the AVD.
Either of those should fix the issue, you don't need to do both.
I know there is many question about it already.
But today I found this:
My phone is Gallaxy Note 4 and Samsung Gallery app works just as I want
'open App permissions page from Activity!' not setting-detail page.
Anyone knows how to do that?
Here is a method to open Settings > Permissions for your application:
public static void openPermissionSettings(Activity activity) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + activity.getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
You should consider using this when your permission is denied and shouldShowRequestPermissionRationale(Activity activity, String permission) returns true.
Hi I want to browse to a file explorer and select a pdf or image present in some directory.
I want the code to do the same.
the below code takes me to gallery and help me choose image but I want to move to file explorer then select file and accordingly I want the code in onactivityResult after selecting.
browsePic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
);
startActivityForResult(i, LOAD_IMAGE_RESULTS);
}
});
I believe you can throw out an open intent for a file chooser using the following.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
try{
startActivityForResult(intent, LOAD_IMAGE_RESULTS);
} catch (ActivityNotFoundException e){
Toast.makeText(YourActivity.this, "There are no file explorer clients installed.", Toast.LENGTH_SHORT).show();
}
The trouble is however, this assumes your user has a file browser open to accepting intents installed on their device, when often no such apps are installed on a device by default.
As in the code above, you may have to throw up a dialog if no Activities exist that can accept this intent, explaining that they need to install a file browser. You could even recommend one that you know works with your application.
I hope this helps.
i think you could do something like this
File strDir = new File("/mnt/"); // where your folder you want to browse inside android
if( strDir.isDirectory()){
//do something
}
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
So I'm making a wallpaper and I want the user to select a folder. So I
have a button in the preferences that launches an intent to open an
image, but what I want is actually just a directory (I guess in the
worst case i can strip the filename from the end). So that's my first
problem: what's the best way to select a folder only?
The second problem is how do I get notified of when the intent is
complete?
public class FilePreference extends DialogPreference implements
View.OnClickListener
{
public void onClick(View v)
{
// open up a gallery/file browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getContext().startActivity(Intent.createChooser(intent, "Select Folder"));
}
use Activity.startActivityForResult and override Activity.onActivityResult
Okay that helped a bit, I managed to work out my wallpaper service was actually an activity. SO then I had to find my preference by name and add a pointer to it so the preference could use startActivityForResult and onActivityResult