Store data on device on application install without opening app - java

Is it possible to save data to a device upon the installation of my application prior to the opening of the application?
If not, I'm looking for a way I can somehow mark a mobile device as "test device" without my application ever being opened on it (only installed).

If you want to distinguish between the shipped devices and every other device, use the build variants. You can add a variable to the BuildConfig class in gradle and specify a value for this sort of "prerelease" you are making and other for the build you are shipping to the store. Then, in the relevant code, check the value of the variable you created in BuildConfig.

Related

How to reuse an Android project to have two different APK

I have previously worked on a project using Android Studio. Now I want to start a new project, but with similar characteristics, so instead of re-starting the project, I have copy the folder, changing the name and made few functionalities.
Nevertheless, when I have built the APK, and tried to reinstall it, my device prompt the following message:
Do you want to install an update to this existing application? Your existing data will not be lost
Before pressing accept, I would like to know if this is going to remove the application that I had in my device with the first project that I was working with or not. If so, I would like to know how to do it, or at least how to avoid this issue.
I assume that it could be done by changing the id of the project, or any other simple solution, but it would be nice to double check
You need to change the application id (package name). Android uses such value to uniquely identify each existing app.
Every Android app has a unique application ID that looks like a Java package name, such as com.example.myapp. This ID uniquely identifies your app on the device and in Google Play Store. If you want to upload a new version of your app, the application ID (and the certificate you sign it with) must be the same as the original APK—if you change the application ID, Google Play Store treats the APK as a completely different app. So once you publish your app, you should never change the application ID.

How do I limit the usages of an Android app

I’m building app which consists on a test. It can be done only once, so I’d like to create a system which checks if the app has been opened before and if the answer is yes then it prevents to open it again. I was thinking to write a file inside the app as soon as the user open it the first time, so on the next startup I can check if it exists or not, but if the user deletes and reinstall the app it doesn’t work anymore. How can I improve this method?
Are you constrained to using local storage? Or can you use a cloud based solution?
If you can use the internet, you could just store device IDs or some other form of unique identification that is checked every time the application is opened. If the ID already exists in your remote store, don't open the app etc.

Java/Android - Best way to update apps that are not in the Play Store

I am installing security software/hardware into a couple different school districts. The application is at it's final stage, however I will need to send updates to users periodically. For example, a general password will be changed for the application every 6 months.
Installing an .apk is considered an "update" after the initial application is installed, correct?
I just have a feeling that there should be some easy way of doing this. I don't really want to give people an .apk. Someone could get smart and tear it apart to find the contents. That, and some others might not understand how to install files on their phone.
What are your ideas? Maybe a web link a user can go to that starts the install for them?
You have multiple misconceptions how updating, APKs and keeping keys secure work.
You have to host your APKs somewhere. Github releases is a pretty common way (but slow), but you could also use google drive, dropbox or your own server.
Your app has to fetch the server regularly and check if a new APK is available (pull-based). Second option is to use push notification in some kind e.g. FCM (push-based). Then you download the APK and let the user install it. Your app cannot start a installation by itself, it has to be done by the user.
But you can redirect the user to the installation menu with that APK, so he just has to click "Install". "Install from unknown sources" has to be enabled for that, if not the user will get an information about that from the OS with a way to enable.
There are apps like "APK extractor" which get you the APKs from google play without root, so there's nothing wrong about giving out the APK. Your APK should never contain secure keys which the user isn't allowed to see. It's easy to reverse engineer those keys, it's just a matter of time.

Do something before app removing

I've got an app that stores few files on sd card. I want my app to remove those files, if user wants to delete the application. How can I do that? Is there a method like onDelete() or something?
I've got an app that stores few files on sd card. I want my app to
remove those files,
Don't store them directly on the sdcard. Use the app's cache space or the directory pointed by getExternalFilesDir. Both are cleaned up by the system when the app is uninstalled
No, there is no way for you app to know when it is being deleted/removed from a device.
Intent.ACTION_PACKAGE_REMOVED
Broadcast Action: An existing application package has been removed from the device.
The data contains the name of the package. The package that is being installed does not receive this Intent.
This comes mostly because if a code would be executed on the app that was about to be removed some might prevent removal etc.
You should provide an option to your users to wipe sdcard data, or use another storage option (that is linked to your app), but I guess you're using the sdcard on purpose.

How to read database of another application

I am working on a rooted device.
I have connected with the adb shell from my pc and I can verify that the other database exists and I can query its tables.
However in the my java code when I try to open the database I get error
android.database.sqlite.SQLiteException: unable to open database file
I am trying to open the database like this:
SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY);
The path is something like /data/data/com.xxx.xxx/databases/xx.db
Am I supposed to read the databases of other applications like this or there is another way?
UPDATE:
I also tried making the system app as adviced in this thread Push my apk to /system/app
I think it works because the app cannot be uninstalled after that from the applications manager like the ordinary apps so I think it is a system app. But still I cannot access the database though I am sure the paths and permissions are ok.
Android OS by default does not allow applications to access each others private folders. To be able to read files that are in another applications folder either:
1) you need to have both applications installed using same user id. Add this to both manifests:
android:sharedUserId="com.xx.xx"
2) If other app is a 3rd party app then your only choice is to install your app as system application. To do that you need a rooted phone and you need to mount the system folder. And then you need to put your application into /system/app/ folder and reboot the phone. It should be installed automatically during the boot.
I would assume that the permissions on the database files are set such that your application has no acess. Even if your device is rooted it doesn't mean that your application is running as root.
This is because the app needs root, and needs to change the permissions of the database you are trying to access so that you can actually access it. What you will need to do is, as root, change the permissions of the database so that everyone can access it, do what you would like on the database, close the database and change the permissions back.
This does circumvent security of android, so proceed at your own risk. You can find a code sample at:
http://rratmansky.wordpress.com/?p=259&preview=true

Categories

Resources