How would I implement expansion packs on the market? Like if I wanted users to be able to purchase more images, how would I do that and also have the main app recognize and load those images? Are there any tutorials you can point me to? I searched using Google but didn't find anything very specific and beginner friendly. Most were in regards to in app billing but that's not what I'm looking for.
You can use PackageManager to look for your expansion packs. The exact way in which you would identify your expansion packs depends on how you're going to use them.
For example, if your expansion packs are runnable in some way - i.e., they all have a main activity - then you'd probably have a custom intent action, and you'd use PackageManager.queryIntentActivities() to give you a list of installed activities that can handle that action.
If these expansion packs are providing just resources and not code, you can choose to either have a "dummy" component that you can identify by intent as detailed above - even if you have no intent of ever running it - or you can look for another approach. For example, if no one but you will be making these expansion packs, you may simply identify them by package name.
PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages();
for(PackageInfo package : packages) {
if(package.packageName.startWith("com.myapp.extension.") {
// found an extension package!
handleExtensionPackage(package);
}
}
If you need to load a resource from an extension package, you'll need to create a context for that package, look up the resource identifier, and then load the resource.
Context extensionContext = createPackageContext(package.packageName, 0);
Resources extensionResources = extensionContext.getResources();
int r_drawable_pic = extensionResources.getIdentifier("drawable/pic", null, package.packageName);
Drawable remotePic = extensionResources.getDrawable(r_drawable_pic);
Related
please help I could not add code, it is throwing error , I'm new.
ActivityResultLauncher<Intent> picActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// your operation....
Uri pic = data.getData();
profile.setImageURI(pic);
}
}
}
It depends on the device's android version. If it's 10 or lower than that, then you can simply save the file path in the Sharedpreference so you can access it later and load the image from there (You can do it in android 10 with requestLegacyExternalStorage of course or you can go with the 2nd option which I provided below).
But, if you're writing the code for android 11 or higher, then there are only three standard ways to do it.
1. Using SAF (Storage Access Framework) :-
You can get the storage access permission of that perticular folder everytime you pick an image from there. But this is not the best option when your app is doing it multiple times. (what if it's photo editor app or social media or something like that?!)
2. Manage all files permission :-
You can go with the All files access permission but it's also too much for the small task and you also have to give clarification to google play when your app has that permission. So it's also the very last option.
3. Accessing from internal app directories - THE BEST WAY! :-
You can go with this option with almost every app!
All you have to do is just take read storage permission, access the file using file descriptor, write it to the internal app directory (it can be either external files directory or cache directory), then you'll have a lifetime access of the image. You can save the path to Sharedpreference and access it anytime.
If you want to save edited image to the gallery then it will also be easy because you already have both read and write permission to that image saved in internal app directory.
That's it. I know the answer is lengthy but it's worth it. :)
I am trying to move from an AbilitySlice to an Ability. I tried the below code,
But was it not working as expected.
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName(getBundleName())
.withAbilityName(MainAbility.class.getSimpleName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);
for moving from an AbilitySlice to Ability in Harmony OS?
Try to Delete Simple from getSimpleName. Like following:
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName(getBundleName())
.withAbilityName(MainAbility.class.getName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);
"Not working as expected" generally is not a valid error description. I'd suspect the AbilitySlice might belong to MainAbility and the whole operation might therefore be pointless, as navigation from A to B could not happen. The example which #Gowtham provided has one small difference (which appears to consider the device on which to launch the Intent with Super Device):
.withDeviceId("")
Have you ever tried to start anything else but MainAbility?
I see that you have mentioned that the package name of your target ability is different than the package name of your ability slice in your reply to #Martin.
Then, you need to make sure the bundleName(or package name) specified in the Intent's Operation builder is having the target ability's package name and not the calling ability's/abilityslice's package name.
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName("enter_package_name_of target_ability_here")
.withAbilityName(MainAbility.class.getName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);
I have a requirement for clearing application user data and cache data for provided package name. Previously I was using clearApplicationUserData(), deleteApplicationCacheFiles() methods from PackageManager class. But now both are deprecated or hidden (#hide) in android 11. What's the alternative?
Below is my previous way of doing it -
PackageManager pm = mContext.getPackageManager();
//observer is ->IPackageDataObserver
pm.clearApplicationUserData(packageName, observer);
and
PackageManager pm = mContext.getPackageManager();
//observer is ->IPackageDataObserver
pm.deleteApplicationCacheFiles(packageName, observer);
What would be the alternate solution for the same? I know there are methods like -
(mContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).clearApplicationUserData()
//for clearing current application user data
and
mContext.getCacheDir().deleteRecursively()
//for clearing current application cache data
But in my case, it is another application and I will provide the package name.
I was reading about clearApplicationUserData() from DevicePolicyManager class, but not sure how it works. Can anyone help me here?
I'm trying to list all apps in android device with queryIntentActivities method but the list doesn't return all the apps , It returns only three of them. Here is my code:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo ri : allApps) {
Log.d("Labels", String.valueOf(ri.loadLabel(packageManager)));
}
is there anyone now why it returns only 3 applications ?
You are most likely trying to do this on Android 11. Make sure you add the <uses-permission android:name"android.permission.QUERY_ALL_PACKAGES"> permission to the AndroidManifest.xml file.
While I haven't tested this aspect of R DP2 yet, it appears that your
app now can't find out what other apps are installed, on a general
basis. The cited example is queryIntentActivities(), but to make this
really work you would need to seriously lobotomize PackageManager. You
can whitelist certain packages and certain structures
to try to get by this for certain use cases. And, this is where the
mysterious QUERY_ALL_PACKAGES permission seen in DP1 comes into play —
this permission removes these new restrictions. Given the "look for
Google Play to provide guidelines for apps that need this permission"
caveat, it is safest to assume that if you try using it, eventually
you will be banned from the Play Store by a bot.
I want to take a picture with the standart system service
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
and AFTERWARDS I want to give it a custom name and directory or path (which should happen in another activity after I've taken the photo with the camera activity)
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
(as suggested in the Google article:https://developer.android.com/training/camera/photobasics#TaskPath)
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
Let me know if you have any idea.
I apreceate any help.
Thank you!
I want to take a picture with the standart system service
Your code is launching any one of hundreds of possible camera apps. The specific app might have been pre-installed by a device manufacturer, or it might be an app that the user installed.
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
Correct.
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
There is no means of having ACTION_IMAGE_CAPTURE give you a "fullsize Bitmap" directly. You can either get a thumbnail-sized Bitmap or have it write a full-sized photo to a location of your choice.
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
No. However, there is nothing stopping you from opening the photo via its file in your activity, then modifying that photo and writing it back out. You could overwrite the original file, or you could write to some new location.
So, for example, if your concern is that you do not want the photo to be in a user-accessible location until your activity is done with it, you could pass a Uri to ACTION_IMAGE_CAPTURE that points to a private location in getCacheDir(), then have your activity write the final version of the photo to a file on external storage.