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.
Related
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.
I have two applications and I want to be able to share a String value between them. For example: user changes the String in app A, when app B is launched, I want it to read the updated String (and vice versa).
I was trying to use SharedPreferences with Context.MODE_WORLD_WRITABLE, but it's been deprecated.
How can I achieve this?
EDIT: App A has to save the value without launching app B. App B has to be able to read that value without launching app A.
I looked at ContentProviders, but they look too complex, especially for a simple String sharing.
One option is use webserver for this. for example store value in web server from app1 and access this value from app2
option two is use content providers. Through the content provider, other apps can query or even modify the data (if the content provider allows it)
Simply put:
one application needs to send intent with data
the other one needs to listen for it with brodcast receiver.
Content provider is probably not the thing you are looking for.
here is good tutorial for brodcast receiver
Try http://developer.android.com/training/sharing/send.html or http://developer.android.com/guide/topics/providers/content-providers.html to share data between 2 applications
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.
I am able to launch an apk from the button click of other apk. but i have to send parameters also.Like if i am giving values in a text and when i click the button then it should send the parameters to other apk as well it should start other apk. I know how to send parameters between activities in the same package. Please guide me how do i do it.
Update:
I have successfully run that apk's. I just had to put package name instead of class name.
Use
i.putExtra(param, value);//Put extra values to your intents, if you are using intents to launch another app.
You need to use content providers to share data across different applications in Android. Here
is an example as to how to use them. You need to have a URI which identifies your dataset and use the same URI in another application to retrieve data from it.
Use Intents, as explained in Android Developer Documentation or many tutorials over the internet.
You should use Bundles to pass parameters, as you can read there.
You can use files in a common directory to comminucate different apps or basicly you can have serversocket in your first app that runs the other one, so the second one can connect it and transfer data to/from first app.
I have been creating Intents using putExtra() for quite a while now and just read in the Android documentation that I should be prefixing the 'name' with the package name. So, instead of 'putExtra( "ButtonText", "Ok")' it should be more like 'putExtra( "com.mycompany.myapplication.ButtonText", "Ok" ).
Is this really necessary? (seems to be OK without it).
If it is necessary what is the advantage?
Also, is the package name the callers or the one being called? If it is the callers the 'called Activity' has to know about the callers name which wouldn't be very generic.
Thanks
Is this really necessary? (seems to be OK without it).
No, it isn't necessary for a completely stand-alone app but may be considered to be good practice anyway.
It is more important in apps which are publicly available so they can interact but maintain some way of uniquely identifying themselves and the data they're exchanging. As for which package name is used will depend on the context.
To give an abstract example...
Company A produces an app which can provide some sort of data processing which apps produced by Company B and Company C can use. The 'action' for the intent will be named relevant to Company A but the data passed into it by the two 'client' apps will be named relevant to the client apps companies. Example...
AppA's docs...
To request data processing use:
com.companyA.intent.action.PROCESS_DATA
Pass data with the above intent as an extra named:
<your package name>.SOME_DATA
Now when the relevant component of AppA is called with the above, it will check that there's an 'extra' with a name which ends with .SOME_DATA but it will also be able to maintain that data separately from other data provided by other apps due to the unique prefix. So...
Company B code
Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyB.SomeApp.SOME_DATA, data);
Company C code
Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyC.SomeOtherApp.SOME_DATA, data);
OK, possibly not one of my better examples but what it comes down to is it's important to see how the Android environment is very much about different application components being able to use each other, pass data and for the source of that data to be uniquely identifiable.
It sounds more like a "best practices" thing than an actual requirement. The benefit you get here is if you stuff a lot of things in generic intents that are handled by several activities spanning several applications, then you're being more specific about the data in your Intent. If you're using these intents only internally in your application and only your own activities are handling them, you're fine the way you are.
I think this is a GREAT idea IF you are wrapping your extended data into a serializable class say com.mycompany.myapp.MyAppData and sending it as:
intent.putExtra("com.mycompany.myapp.MyAppData",myAppData); //==> out
and retrieving it as:
MyAppData myAppData= (com.mycompany.myapp.MyAppData)intent.getSerializableExtra("com.mycompany.myapp.MyAppData",myAppData; // <== in
JAL