get the package name of the application that is calling my app - android

I am developing an application in android that create folders and stored files and images in these folders. Each folder have a list that contains the name of applications that are permitted to access these files and image. my question is what is the code that can give me the package name of the application that is accessing my app in order to compare it with the names of apps in the list of each folder. I succeed in getting the package name when the case is that other application retrieve data from my application (startActivityForResult). but i cant get the package name when the case is just view (startActivity)!!! I need to know the package name in both case. any help?

Sorry. That isn't possible.
When an Activity is started with startActivityForResult(), Android needs to know who to return the result to. It keeps this information in an internal data structure and makes the information available to the called Activity via getCallingActivity() and getCallingPackage().
However, when an Activity is started with startActivity(), Android doesn't store the information about the caller Activity because it doesn't need this information (because Android knows that it isn't going to return a result to the caller Activity) and therefore it isn't available to your Activity.
Note that activities can also be started by Service and BroadcastReceiver component, so there isn't always a "caller Activity".
This is either a flaw in the design of Android or a "security feature", depending on how you want to use it.

Related

In Android, how to add a file to another application

I need to place a file inside of another application's specific folder. Is there a best practice, or related expected behavior for this?
To solve the issue, I have created the desired file during my APK's installation, then written its contents. While this works (since I have root rights), I want to know if is there a way to "request" another application to "create the file themself", this is mostly to guarantee that when that other application is removed, that it removes its files (since my app is the owner of that file).
I am unsure if this is considered good behavior, and could not "phrase" the question in a way that showed related results (I am not an native English speaker).
I need to place a file, inside of another applications specific folder
If by "applications specific folder", you mean internal storage, this is not possible, except perhaps on rooted devices.
I want to know if is there a way to "request" another application to "create the file themselfs"
Not in general. Some developers might have an API for this in their apps.
The official way of sharing data between apps is ContentProviders.
There is no limit to what may back the specific provider - a file, database or some other data source. I think this is the best way to go in your situation.
The approach you have described is indeed strongly advised against. The whole Android security is based on the idea that you can't directly access the data of other apps.
Ok, here is my proposal. I will assume that you can add functionality to the server app or define some technical guidelines for it at least, so that it's developers have to add the functionality.
A - Server App
B - Client App
[B] Create the html file in the common storage
[B] Save its location to some String variable
(Optional) [A] If there are more server apps that the user can choose from, make sure they all have a BroadcastReceiver with a common INTENT_ACTION.
[B] Send a broadcast Intent with the path saved in 2. as an extra value.
[A] Receive the Intent, check if the path in the extra is present.
[A] Get the file from the received path and copy it into the internal storage.
(Optional) [A] Add a BroadcastReceiver to monitor app uninstalls. When notified about the client app being uninstalled, remove the html file you received from it.
This is the basic algorithm, but I guess the implementation is obvious enough. Let me know if it solves your problem.

Clear Application's Data Programatically from another Android Application

I have an android application HELLOWORLD
I am trying to create another android application HELLOWORLDCLEANER which can clear my HELLOWORLD application's data in one click
Application's data is like databases, shared preference files, and other files created within the application
I can clear data in the mobile through Settings->Applications-> ManageApplications-> My_application->Clear Data
But I don't want go every time to Settings page and clear the HELLOWWORLD app data. Does android provide such facility to do if I know the package name of HELLOWORLD application?
I have gone through solutions provided in Stackoverflow but it tells how to clear app data of itself not about other apps..
Each Android application lives in its own security sandbox.
By default, the system assigns each application a unique Linux user ID
(the ID is used only by the system and is unknown to the application).
The system sets permissions for all the files in an application so
that only the user ID assigned to that application can access them.
Which means you can't access another app's resources.
The only way you could achieve that would be by executing su commands, but that would require root access.
you don't need to be root to do that as long as you sign the 2 apps with the same key.
Then you can just tell one app to clear its own data from another app.

Receiving Intent EXTRA_MESSAGE from Another Application

In my Eclipse workspace I have my main application: A
and I have another fully functional application: B
I have configured appliction A to open up application B upon the click of a button by using an Intent and it works.
Here is the issue:
In application B I need to receive the EXTRA_MESSAGE. However, I am unable to access the info because application B does not recognize application A:
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE_DESC);
states that "MainActivity" cannot be resolved to a variable.
*addendum: I am working on the actual code of application B, as opening the class reference in application A gives me a uneditable display.
It seems that I would need to alter the manifest of application B, and add application A in the build path of B... this seems messy and not reusablely friendly.
Thank you for any help
Interesting question.
The argument to Intent.getStringExtra() is a String containing the key associated with the extended data. To get that data, you need to know the key, not the name of the app or Activity from which it came. Sometimes, a developer will prepend the app's package name to the key value, but the key is not likely to be the package name alone, or the name of the sending Activity, or the sending app.
If you know how the extended data was stored, find the key value and use it to retrieve the data. For an application that makes its Intents publicly available, you should be able to find this.
Otherwise, you can't retrieve the extended data, by design. This is a security feature to prevent unauthorized apps from reading data from an Intent.
Why not use a shared library between app A and B. In the project.properties file, you can mention the path of this shared lib. using android.library.reference
But for interprocess communication, it is better to use AIDL.

android: questions about ContentProvider

As I understand it, ContentProvider - is the data on the global level of the whole device?
The questions:
1) Is it possible to develop and distribute only ContentProvider (no Activity, ie not as a complete application, but only as data)? Does anybody do so? And when?
As for the user it will look like? What is the difference between build of ContentProvider and build of normal application?
2) If another developer wants to use my ContentProvider, then how he will be able to access the column names and other data necessary to work with my ContentProvider? I have to give the library?
Sorry for my English.
Thanks
1) Is it possible to develop and distribute only ContentProvider (no
Activity,...
You have to add at least one Activity to your App to be launched by user.In fact forsecurityreason all services,receivers,... that you declare in manifest,will not register unless your App run explicitly by user and this needs to a Main/Launcher Activity.So you have to add such Activity to your App.
2) If another developer wants to use my ContentProvider,...
You have to publish documentation in about your App.
1) Is it possible to develop and distribute only ContentProvider?
Ans:- You need to add at least one activity to your application and all the resource in the menifest will be registered once your app will be launched explicitly.
2) If another developer wants to use my ContentProvider..
Ans:- You need to provide the proper documentation for accessing the content. Other developer can use the content of your app b using the URI which is defined with your ContentProvider.

How to share data among applications when we don't know which app is provider but first app has stored data

I have 5(any number) android applications which will share some data.
Now when 1st/2nd/3rd..(i couldn't know) android application will get installed by user, it will save a string in the memory(internal memory),
When any other application will get installed, it should know that one of the app has already written data and now it should act as user of this data.
To sum up, first app installed will act as provider and others as users...but all the applications can be potential provider and other as users..
Is this feasible?
i tried using file/shared preference/content providers...but in file, you should know file path before using it which is dependent on package name..
In shared preference, we have to create package context..
In content provider, we have to import provider package..
My real motive is, actually in each app, i register the user first time...
now i want , if user install 2nd app from me, it can just use data of first app and do not ask for register..
but the problem is i don't know which app will be installed first by user..
Please help if i can use some other approach??
i found one thread related to this...
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/W-oOa0x9amg
http://android.bigresource.com/Android-How-to-share-data-between-applications-with-no-dependency-HRMMGMIkL.html
This thread pretty much answers my question, in terms of feasibility and options i have for this scenario.

Categories

Resources