Reputation: 403
I have two applications, one works as a main app, and the another as a service. In the service app manifest file, I configure a service like:
<service android:name=".services.FirstService">
<intent-filter>
<action android:name="ch.service.action.FIRST_SERVICE"/>
</intent-filter>
</service>
And in the main app, I start a service like:
Intent intent = new Intent("ch.service.action.FIRST_SERVICE");
startService(intent);
Here, I have to duplicate "ch.service.action.FIRST_SERVICE". How can I avoid this? How can I share some common constants, for example define one in service app, and can be retrieve in main app?
Any answer is appreciated.
Upvotes: 5
Views: 276
Reputation: 739
There are different ways to archive the same thing, but why not keep it simple:
Do you have a reference to the service project? if it so, why not just create a setting in the service project and reuse it on the main one when create the intent:
Intent intent = new Intent(cxt.getString("service_string", default value));
or, like I did in my apps, just create a public static final variable on the service class and access it from the main app?
public static final String ACTION_FIRST_SERVICE = "ch.service.action.FIRST_SERVICE";
Upvotes: 0
Reputation: 8478
You should use SharedPreference
between both of your application. In your Service application you should write
the String
ch.service.action.FIRST_SERVICE to SharedPreference
:
SharedPreference sharedPreferences = getSharedPreferences("any_name", Context.MODE_WORLD_READABLE);
SharedPreference.Editor mEditor = sharedPreferences.edit();
mEditor.putString("service_string","ch.service.action.FIRST_SERVICE");
mEditor.commit();
When you want to get the value from other application, you first need to get Context
of your service application :
Context mServiceAppContext = createPackageContext("com.example.service_app_package", 0);
You can use mServiceAppContext
to get the String value :
SharedPreference sharedPreferences = mServiceAppContext.getSharedPreferences("any_name", Context.MODE_WORLD_READABLE);
// Context.MODE_WORLD_READABLE is important, it makes the value readable across other applications
sharedPreferences.getString("service_string", /* default value */ "service_string","ch.service.action.FIRST_SERVICE");
Best Explaination is given at : http://thedevelopersinfo.com/2009/11/25/getting-sharedpreferences-from-other-application-in-android/
Upvotes: 1
Reputation: 219
If you can merge two applications together, certainly you can share anything you want. Actually one application can contains many service and intent service.
If you have some reason to separate one application into two, or they are two irrelevant apps, using intent to communicate always need class name which is unavoidable I think.
Upvotes: 0
Reputation: 559
for something like this you have to create a library project and include it to both your main and service application and define your name in the library you included. Hope this helps, not sure if there is any other better way :)
Upvotes: 0