Android is a shared resource between two applications

I have two applications: one works as the main application, and the other as a service. In the service application manifest file, I configure the service, for example:

<service android:name=".services.FirstService"> <intent-filter> <action android:name="ch.service.action.FIRST_SERVICE"/> </intent-filter> </service> 

And in the main application, I start the service, for example:

 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 a service application and can be found in the main application?

Any answer is welcome.

+5
source share
4 answers

You must use the SharedPreference between your application. In the Service application, you must write String ch.service.action.FIRST_SERVICE before 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(); 

If you want to get the value from another application, you first need to get the Context 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"); 

The best explanation is given at: http://thedevelopersinfo.com/2009/11/25/getting-sharedpreferences-from-other-application-in-android/

+1
source

for something like this, you need to create a library project and include it in both the main and service applications and determine your name in the library that you included. Hope this helps, not sure if there is another better way :)

0
source

If you can combine the two applications together, you can share everything you want. In fact, one application can contain many services.

If you have a reason to separate one application from two, or these are two irrelevant applications, using the intention to communicate always requires a class name, which, in my opinion, is inevitable.

0
source

There are different ways to archive the same thing, but why not just keep it simple:

  • Do you have a link to a service project? if so, why not just create a parameter in the service project and reuse it on the main when creating the intent:

    Intent intent = new Intent (cxt.getString ("service_string", default value));

or, as in my applications, just create a public static end class variable of the service class and access it from the main application?

 public static final String ACTION_FIRST_SERVICE = "ch.service.action.FIRST_SERVICE"; 
  1. If you don’t have a link to the service project and you don’t want to add, the best way is to expose it to someone ... or create a lib project that uses
0
source

All Articles