Android ringtone picker refresh the activity, why? - java

in my app i have some layout to edit a task and select a RingTone and save it into the database.
When i update the task, i one also to update the ringtone name. I use for that the ringtone picker, start a new Intent and get the selected ringtone uri inside the onActivityResult() method.
The problem ist, every time when i click to pick the new RingTone the page reload and i lose all the populated data in my formular, that come from the database.
How can i solve this problem. Is there a way to open the ringtone picker without reload the complete activity after selecting the ringtone?
Here is how i open picker:
protected void openRingtoneDialog() {
final Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.ringtone_choose));
if (mAlarmTonUri != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, mAlarmTonUri);
}
else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
}
startActivityForResult(intent, RINGTONE_RESULT);
}
and here, how i get the selected ringtone.
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
String ringTonTitle = "";
// Get the result from RingtoneActivity
if (resultCode == RESULT_OK && requestCode == RINGTONE_RESULT) {
mAlarmTonUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (mAlarmTonUri != null) {
mAlarmTonValue = mAlarmTonUri.toString();
ringTonTitle = RingtoneManager.getRingtone(this, mAlarmTonUri).getTitle(this);
}
else {
ringTonTitle = "unknow";
}
mAlarmTonTextView.setText(ringTonTitle);
}
Thank you for your help!

I ran into this in my app. I had overridden onResume to reload my view in case changes were made while my app wasn't on top. Coming back from the ringtone picker was setting off my onResume. For me, it was safe to move everything from onResume to onStart. There it would run when my app came to the foreground or first started but not when the ringtone picker was dismissed.

Related

Using mKeyguardManager for lockscreen only displays when opening the application

In Java, I am trying to implement a feature where only the admin (person who knows the device password) can access an information screen and when they click on a button within the app in the MainActivity, the lock screen will appear as a form of authentication and display another activity screen on success. Is this possible?
So far, I noticed that the authentication only displays when I open the app and not when I press the button in an onClickListener. Most of the solutions I've seen are doing it this way. I have the code in MainActivity within an onCreate() method.
MainActivity
ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
} else {
finish();
}
}
});
start_end_button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
authScreen(activityResultLaunch);
}
});
private void authScreen(ActivityResultLauncher<Intent> activityResultLaunch) {
KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (!mKeyguardManager.isKeyguardSecure()) {
// Show a message that the user hasn't set up a lock screen.
} else {
Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);
if (intent != null) {
startActivityForResult.launch(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
Intent intent = new Intent(DetectorActivity.this, SearchActivity.class);
startActivity(intent);
finish();
}
Currently, the app immediately goes to the SearchActivity class without having the lock screen displayed in between. Even if I get into the app after PIN code entered is a success, it still doesn't get into the activityResultLaunch success condition as per the new implementation referenced here by user Martin Zeitler.
Reference:
https://mobile-security.gitbook.io/mobile-security-testing-guide/android-testing-guide/0x05f-testing-local-authentication#:~:text=In%20Android%2C%20there%20are%20two,and%20the%20Biometric%20Authentication%20flow.

Show Progress Dialog while Processing OnActivityResult

I have written below codes where when user click attach button to select photos.
Below is code for same.
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select file to upload "), 1);
Below is code for OnActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (null != data) { // checking empty selection
if (null != data.getClipData()) { // checking multiple selection or not
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
Log.i(TAG, "Path" + getPath(uri));
filespath.add(getPath(uri));
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(getPath(uri), options);
bitmaps.add(bitmap);
}
} else {
Uri uri = data.getData();
}
}
}
}
Now user can select multiple photos and I realized that when photos are more than 10, I get warning too much work done on main thread. when user click on Done button after selecting photos, I have Recycler view where I am showing thumbnail of images selected by user before final upload.
Now issue is when user click Done and till it shows Thumbnail, how can I show ProgressDialog , handle freeze screen and avoid warning work done on main thread.
To keep the parsing and loading work off the main thread you can just wrap everything in an AsyncTask. Given the small amount of code shown, I don't know what functions do what in the above, so this might have to be adjusted a bit. Move all the parsing logic, etc to do in the background as such:
AsyncTask<Void, Void, List<Bitmap>>() {
#Override
protected void onPreExecute()
{
//show progress dialog
//Are you trying to prevent the user from clicking on the UI while updating?
//You can do something such as:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
#Override
protected List<Bitmap> doInBackground(Void... voids) {
//perform the logic, this will return a list of all the Bitmaps to onPostExecute
//Do NOT perform any ui logic here.
}
#Override
protected void onPostExecute(List<Bitmap> bitmaps) {
//cancel progress dialog.
//Update the UI with the response.
//Clear the window lock:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
};

Activity is recreated after resuming app from task manager list

I have an activity where we click image using,
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPicture");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, 2);
and have activity for result as,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
mImageView.setImageBitmap(mImageBitmap);
}
}
after clicking and saving photo onDestroy() is called so, I have used onSaveInstanceState(), onRestoreInstanceState() to resume same activity without recreating it when resumed from background(kept in background for 1 min or 10 secs) still the activity is not restored.
Issue occurs in low memory device currently using Samsung J1 with Version: 4.4.4, RAM:512 MB
How to solve this? Please help thanks in advance.
I have encounter this once,
The problem with your device option
1.got to settings
2.find the developer options
3.In Apps section check the Don't keep activities is false..
what it means image picker is detached form activity as new activity.
once user leaves the old activity it will kill by option on settings.
Check you may found..

Draw Overlay permission only asks first time when app is installed

I'm developing a music player application. In which, I want to display a small floating button on screen for play/pause event when app is minimized(paused). (Functionality like face book messenger chat head). It works perfectly fine on devices which has SDK < 23. But in Device with SDK >= 23, It asks for permission only 1st time when app in installed.
If permission is granted, it also perfectly displays floating button. But once the app is closed and again started, It is not showing floating button any more.
my code for opening permission dialogue box is:
public final static int REQUEST_CODE = 100;
public void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/** check if received result code
is equal our requested code for draw permission */
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
// continue here - permission was granted
if(isServiceRunning != true){
isServiceRunning = true;
intent12 = new Intent(this, notificationService.class);
bindService(intent12, notificationConnection, Context.BIND_AUTO_CREATE);
startService(intent12);
}
}
}
}
I'm displaying floating button when app is paused(when home button or back button is pressed to minimize the app). So I'm calling this checkDrawOverlayPermission() method in onPause() method of my apps mainActivity, like this:
#Override
protected void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT >= 23) {
checkDrawOverlayPermission();
} else {
if(isServiceRunning != true){
isServiceRunning = true;
intent12 = new Intent(this, notificationService.class);
bindService(intent12, notificationConnection, Context.BIND_AUTO_CREATE);
startService(intent12);
}
}
}
But I can't understand what I'm missing here. I think code for checking permission is ok because first time it displays floating button on the screen along with asking permission in a small dialogue. Please help. Thanks!
You are not doing anything if the permission is already given in SDK > 23.
Add an else part to the checkDrawOverlayPermission method like this.
public void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) { // WHAT IF THIS EVALUATES TO FALSE.
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
} else { // ADD THIS.
// Add code to bind and start the service directly.
}
}

Fragment gets destroyed 4 times when returning from Camera Intent

I have a simple fragment that when user taps on the screen it will send an Intent to open the Camera app and then expect an image to be returned.
public void camera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap)extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
According to Google developer this should be all that is required.
After the picture has been taken and I press SAVE button in Camera app it returns back to my app.
I have set some breakpoints in onDestroy() and in onActivityResult(), it first destroys the fragment and creates a new one 2 times, then onActivityResult() is called and then it is .... destroyed again ... and created. So the image gets lost.
Why is this happening and how can I fix it?
Running on Samsung S4
I found a dirty fix to it (dirty imo looks pretty weird)
In the AndroidManifest.xml add this parameter to your Activity that handles the Camera Intent:
android:configChanges="orientation|screenSize"
The Activity gets destroyed because of the orientation change in the transition to and from the Camera app... with this parameter it wont destroy.
Did u try using an intent extra called EXTRA_OUTPUT which will store the photo in given uri
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent , Request_Id);

Categories

Resources