Android plug-in development and Android plug-in development

Source: Internet
Author: User

Android plug-in development and Android plug-in development

I found a lot of plug-in-type development information on the Internet the most worthy of research significance of the two open source development plug-in framework http://www.oschina.net/p/cjframeforandroid and http://www.oschina.net/p/xcombine in addition to a very simple way to achieve through sharedUserId. The following describes how to implement sharedUserId in the most convenient way.

Idea: set the same sharedUserId in the primary app and the secondary app, find the package name of the other apps through this id in the primary app, and enable other apps through the package name.

Note that the action of the main activity in the app is the package name and the category is set to the default value.

Demo http://download.csdn.net/detail/u012303938/8642017

Code: The main app calls an app (plug-in) using a button ).

MainActivity. class

Package com. example. plugin; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. intent; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. graphics. color; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; Import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button button1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. button1); button1.setOnClickListener (new OnClickListener (){ @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubattachPlugin (findPlugins () ;}} private List <PluginBean> findPlugins () {List <PluginBean> plugins = new ArrayList <PluginBean> (); // retrieve the package name to obtain the plug-in PackageManager pm = getPackageManager (); List <PackageInfo> pkgs = pm. getInstalledPackages (PackageManager. GET_UNINSTALLED_PACKAGES); for (PackageInfo pkg: pkgs) {// package name String packageName = pkg. packageN Ame; String sharedUserId = pkg. sharedUserId; // sharedUserId is agreed upon during development, so that you can determine whether it is your own if (! "Main. plugin ". equals (sharedUserId) | "com. example. plugin ". equals (packageName) continue; // process name String prcessName = pkg. applicationInfo. processName; // label, that is, the appName has a String label = pm. getApplicationLabel (pkg. applicationInfo ). toString (); PluginBean plug = new PluginBean (); plug. setLabel (label); plug. setPakageName (packageName); plug. setPrcessName (prcessName); plug. setSharedUserId (sharedUserId); plugins. add (plug);} re Turn plugins;} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as y Ou specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);} private void attachPlugin (List <PluginBean> plugins) {Intent intent = new Intent (); if (plugins! = Null & plugins. size ()> 0) {intent. setAction (plugins. get (0 ). getPakageName (); Toast. makeText (this, plugins. get (0 ). getPakageName (), 1 ). show (); startActivity (intent);} else {Toast. makeText (this, "no plug-in found", 1 ). show ();}}}

Store plug-in data pluginbean. class

package com.example.plugin;public class PluginBean {private String pakageName;private String label;private String sharedUserId;public String getSharedUserId() {return sharedUserId;}public void setSharedUserId(String sharedUserId) {this.sharedUserId = sharedUserId;}public String getPrcessName() {return prcessName;}public void setPrcessName(String prcessName) {this.prcessName = prcessName;}private String prcessName;public String getPakageName() {return pakageName;}public void setPakageName(String pakageName) {this.pakageName = pakageName;}public String getLabel() {return label;}public void setLabel(String label) {this.label = label;}}


The primary app sets the sharedUserId in the configuration file.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.plugin"    android:sharedUserId="main.plugin"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.plugin.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

The app writes an activity that loads the gridview.

MainActivity. calss

package com.example.plug_test;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.GridView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {private GridView gridView1;    private ArrayList<Map<String, Object>> list=new ArrayList<Map<String,Object>>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findView();    }    private void findView() {// TODO Auto-generated method stub    for(int i=0;i<5;i++){    Map<String, Object> map=new HashMap<String, Object>();    map.put("apkName", "name"+i);    map.put("icon", R.drawable.ic_launcher);    list.add(map);    }    gridView1=(GridView) findViewById(R.id.gridView1);    SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.grid_item,    new String[]{"apkName","icon"}, new int []{R.id.tv_name,R.id.img});    gridView1.setAdapter(adapter);}@Override    public boolean onCreateOptionsMenu(Menu menu) {                // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

Set the ID in the configuration file, set the main activity intent package name, and hide it

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.plug_test"    android:sharedUserId="main.plugin"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.plug_test.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="com.example.plug_test" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>    </application></manifest>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.