📜 ⬆️ ⬇️

Create a modular application

Did you want to make a browser with plugins, a program with themes, a game with addons or some other modular Android application? But how to do that? I will tell you how to make a simple application that will receive text from modules.

It is possible that what is written here is crutches, but on dev.android.com and in Google on this topic I did not find anything. Those who do not like to read too much, can immediately disassemble the code .

Theory


What I came up with, looking at the ADW Launcher theme template, is to send special requests to the module. That is, the module in the <intent-filter> <action> name parameter will have a value of type my.app.GET_DATA, and the application will send start requests using startActivity (startActivityForResult) and startIntentSender (startIntentSenderForResult). I want to remind that one Activity can have several <intent-filter> tags.

Practice


Let's start with the programming of the application itself. The basis of the application is not much more complicated than Hello World, so I will only tell you how to work with modules.
')
The easiest preview option:
 // "mod.ul.ed.MODULES" - action    intent-filter' Intent call = new Intent("mod.ul.ed.MODULES"); startActivity(call); 
After calling startActivity, Android will ask the user which module to open and upon selection (you can assign a default module) will open.

To open a specific module (and its specific Activity), you can use the following code:
 Intent call = new Intent("mod.ul.ed.MODULES"); // mod.ule.first -   // mod.ule.first.ActivityMain -  Activity   call.setClassName("mod.ule.first", "mod.ule.first.MainActivity"); startActivity(call); 
You can also pass parameters to the module, see the description of the Intent class for this.

To get data from the module it is better to use startIntentSenderForResult:
 Intent call = new Intent("mod.ul.ed.GET_TEXT"); //    ,         call.setClassName("mod.ule.first", "mod.ule.first.MainActivity"); IntentSender sender = PendingIntent.getActivity(getApplicationContext(), 1, call, 0).getIntentSender(); try { startIntentSenderForResult(sender, 1, call, 0, 0, 0); } catch (SendIntentException e) { } 
If you use startActivityForResult, then the user will be able to see the flashing of another window (Activity).

You may also need to get a list of modules. This is implemented using PackageManager.queryIntentActivities (Intent intent, int flags) :
 //   Activity,   action = "mod.ul.ed.MODULES" Intent intent = new Intent("mod.ul.ed.MODULES"); //    Activity List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0); 

Let's start creating a module. First, in AndroidManifest.xml for the necessary Activity we add the necessary actions, for example the action GET_TEXT:
 <intent-filter> <action android:name="mod.ul.ed.GET_TEXT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 

You need to return something after checking the action, you can do it like this:
 //  action if(getIntent().getAction().equals("mod.ul.ed.GET_TEXT")) { //   Intent data = new Intent(); data.putExtra("text", "This is first module"); setResult(RESULT_OK, data); //   finish(); //  Activity return; //    } 


[UPD] For creation, it is better to get the topic resources through getPackageManager (). GetResourcesForApplication (...) and use them.

Total


So it is easy to make a modular application. For those who did not see the link at the beginning: the application code and 2 modules can be viewed on googlecode . As I wrote at the beginning, this is probably a crutch. If someone knows a more elegant and correct solution, please tell us about it.

Source: https://habr.com/ru/post/123306/


All Articles