Android 9 Intent.ACTION_UNINSTALL_PACKAGE not working - android

On android pie, I want to call the package manager to uninstall my own app. Here is what I am trying:
val uri = Uri.parse("package:$packageName")
val uIntent = Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri)
startActivity(uIntent)
Oddly this is not working. Nothing is being shown in the logcat as well.
I have also tried ACTION_DELETE
val uri = Uri.parse("package:$packageName")
val uIntent = Intent(Intent.ACTION_DELETE, uri)
startActivity(uIntent)
Please tell me what I am doing wrong. This seems a pretty straightforward job. Am I missing any permission or something I need to declare in manifest?
Thanks.

I was missing manifest permission.
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
This is probably required for Android 6.0 and above. The code in the question now works perfectly. Tested on Android 9 and Android 10.

Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.example.myapplication"));
startActivity(intent);

Related

ActivityNotFoundException crash when starting "open url" intent on Chrome 86.0.4240.75

Chrome for Android version 86.0.4240.75 was released just a few days ago. The rollout has barely started, but we're seeing a new crash whose numbers are going straight up, on devices using that version of Chrome / Android System WebView.
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.parse(url), "text/html")
intent.addCategory(Intent.CATEGORY_BROWSABLE)
startActivity(intent)
It looks like for any url, on any device using version 86.0.4240.75, the above code crashes with ActivityNotFoundException:
android.content.ActivityNotFoundException: No Activity found to handle Intent
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1854)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1544)
Seems like a bug in Chrome, but has anyone figured out a workaround yet?
A bit surprising, but I found an easy workaround. Actually two: remove the MIME type or remove the category.
This works fine even with 86.0.4240.75:
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
intent.addCategory(Intent.CATEGORY_BROWSABLE)
startActivity(intent)
This does too:
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.parse(url), "text/html")
startActivity(intent)
(I'm not sure what the benefit of defining text/html or CATEGORY_BROWSABLE was in the first place, as neither seems mandatory.)

Why I can't open downloaded file on Android Q

I have a simple functionality in my application, user can download the file and then open it or share using any other installed application. Before android Q I hadn't any trouble with this, but with new privacy changes, I don't know how to do this correctly.
I need to do the next two steps:
Download file, using DownloadManager, in the common device Downloads folder (not in the application downloads folder)
Open this file or share this file (using intent)
To download the file to common Downloads folder I use next code
val request = DownloadManager.Request(config.uri)
.setTitle(title)
.setDescription(config.description)
.setNotificationVisibility(notificationVisibility)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename.pdf")
val downloadId = downloadManager.enqueue(request)
and after downloading done I getting downloaded file Uri like this
val query = DownloadManager.Query().setFilterById(downloadId)
val cursor = downloadManager.query(query)
val fileUriStr = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
val fileUri = Uri.parse(fileUriStr)
In the end, when user click on button "Open", I creating next intent
val mime = context.contentResolver.getType(fileUri)
val intent = Intent()
.setAction(Intent.ACTION_VIEW)
.setDataAndType(uri, mime)
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
All these solutions work properly until Android Q. On android Q after Intent start I see the dialog to chose target application, when I pick target application I get error that this application cant access the file (I use Adobe Reader as target application).
So, the question is how to do this operation correctly on Android 10?
P/S/ Sorry for my bad English
I had a same problem. I used DownloadManager.getUriForDownloadedFile() instead of the Uri returned from query and the problem solved! I don't know what exactly causes the problem but I found that this solution doesn't work on lower versions. So I ended up with this line:
var fileUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) downloadManager.getUriForDownloadedFile(requestId) else Uri.parse(fileUriStr)
Also I get the mime type from here:
val mime = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE))
And it works on all versions.

Uninstall application programmatically Android 10

I am developing an application which will uninstall itself after a button click. The following code works for the uninstallation of application running in Android.
Uri uri = Uri.fromParts("package", getClass().getPackage().getName(), null);
Intent uninst_intent = new Intent(Intent.ACTION_DELETE, uri);
startActivityForResult(uninst_intent, EXIT_REQUEST);
But this is not working for the new versions of android such as Android 9 and 10. This Action intent is not deprecated in the newer APIs. What am i missing here?
There is permission missing in Manifest please add this permission in manifest...
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"></uses-permission>
this will work...!!!

Camera doesn't work for android 7.0

I'm getting: android.os.FileUriExposedException.
When targeting Android N, file:// URIs are not allowed anymore. I know
We should use content:// URIs instead. However, my app needs file for
both image and video. Any ideas?
mMediaUri = Uri.fromFile(new File(AppHelper.getDirectoryPath(),AppHelper.getFileName() + ".jpeg"));
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
iCamera.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(iCamera, Constants.INTENT_CALL.CAPTURE_IMAGE);
and onActivityResult
case Constants.INTENT_CALL.CAPTURE_IMAGE:
String filePath=SiliCompressor.with(getActivity()).compress(mMediaUri.toString(), true);
Please add sample code...if Available.
After some research finally got relevant answer to my question just set min target sdk version to 23.

Android photo picker for lollipop and above

I'm using the following code for picking photos:
Intent intent =new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 100);
and return the result with following:
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap bmp1 = BitmapFactory.decodeStream(inputStream);
This works good on pre-lollipop but it says "Unfortunately the app has stopped" on lollipop and above. Do I need any permissions on lollipop and above? Has something changed since lollipop? Please guide me/ Suggest a simple method to pick photos on lollipop and above.
Use following way
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, Const.PICK_IMAGE_REQUEST);
OR use custom layout and activity
I have developed it.Please check below link working in all android version 4.0 or above. you dont need to ask for permission as its target version is 22.
https://github.com/rajscet/Photo_Picker_Git
Do I need any permissions on lollipop and above?
You should hold READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE. Note that on Android 6.0+, if your targetSdkVersion is 23 or higher, you need to use runtime permissions to request this permission from the user, as these permissions are dangerous.

Categories

Resources