Android plug-in development-Verification of four components and Application functions in OpenAtlas
With OpenAtlas for plug-in development, plug-in development can be performed almost according to the normal program development process, without adding additional things. To verify that the four components work properly, write a plug-in to verify its functionality. In addition to the four main components, most applications also have Application classes. This class also needs to be verified.
First, create a new module and follow the normal process for development. Create an Application class. For convenience, all the verifications use the log output form.
public class App extends Application { @Override public void onCreate() { super.onCreate(); Log.e(TAG, Plugin named Component has init!); Log.e(TAG, ===Plugin Application is=== + this); }}
The verification of the Activity is no longer repeated. The previous articles are based on the Activity or Fragment. If you are interested, you can go to the relevant link.
-Android plug-in development: OpenAtlas initial experience
-OpenAtlas for Android plug-in-based development to generate a list of plug-ins
-Android Plugin-Based Development: OpenAtlas resource packaging tool patch aapt Compilation
-OpenAtlas plug-in adaptation for Android plug-in development
-Android plug-in development: solving the host registration problem of OpenAtlas Components
Next, verify the Service and create a new Service. register the configuration file to the end.
public class ComponentService extends Service { @Override public void onCreate() { super.onCreate(); Log.e(TAG, ===Plugin Service onCreate===); } @Override public IBinder onBind(Intent intent) { Log.e(TAG, ===Plugin Service onBind===); return new BinderImpl(); } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, ===Plugin Service onUnbind===); return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, ===Plugin Service onStartCommand===); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG, ===Plugin Service onDestroy===); } class BinderImpl extends Binder{ BinderImpl getService(){ return BinderImpl.this; } void testMethod(){ Log.e(TAG,BinderImpl testMethod); } }}
Then ContentProvider. All are empty implementations, only output logs.
public class ComponentContentProvider extends ContentProvider { @Override public boolean onCreate() { Log.e(TAG,ComponentContentProvider onCreate); return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.e(TAG,ComponentContentProvider query:+Arrays.asList(projection)+ +Arrays.asList(selectionArgs)+ +selectionArgs+ +sortOrder); return null; } @Nullable @Override public String getType(Uri uri) { Log.e(TAG,ComponentContentProvider getType); return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { Log.e(TAG,ComponentContentProvider insert:+values); return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.e(TAG,ComponentContentProvider delete:+selection+ + Arrays.asList(selectionArgs)); return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.e(TAG,ComponentContentProvider update:+values+ +selection+ +Arrays.asList(selectionArgs)); return 0; }}
There are two types of broadcast and broadcast: static broadcast, which needs to be registered in the inventory file, dynamic broadcast, and code registration.
public class ComponentBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, ===Plugin BroadcastReceiver onReceive===); }}
public class ComponentBroadcastDynamic extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, ===Plugin ComponentBroadcastDynamic onReceive===); }}
Finally, register these components in the plug-in list file.
data-snippet-id=ext.3d490ef9fc184d473a2aa363079dd76e data-snippet-saved=false data-codota-status=done>
Compile an Activity to verify all functions. The layout of the Activity is no longer pasted. The layout is a layout composed of buttons.
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(TAG, ===Plugin MainActivity is=== + this); findViewById(R.id.start_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ComponentService.class); startService(intent); } }); findViewById(R.id.stop_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ComponentService.class); stopService(intent); } }); final ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, onServiceDisconnected()); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, onServiceConnected()); ComponentService.BinderImpl binder = (ComponentService.BinderImpl) service; ComponentService.BinderImpl bindService = binder.getService(); bindService.testMethod(); } }; findViewById(R.id.bind_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ComponentService.class); Log.i(TAG, bindService()); bindService(intent, conn, Context.BIND_AUTO_CREATE); } }); findViewById(R.id.unbind_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unbindService(conn); } }); findViewById(R.id.broadcast).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(cn.edu.zafu.component); sendBroadcast(intent); } }); final ComponentBroadcastDynamic componentBroadcastDynamic = new ComponentBroadcastDynamic(); findViewById(R.id.register_broadcast).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(cn.edu.zafu.component.ComponentBroadcastDynamic); registerReceiver(componentBroadcastDynamic, intentFilter); Log.e(TAG,ComponentBroadcastDynamic registerReceiver); } }); findViewById(R.id.send_broadcast_dynamic).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(cn.edu.zafu.component.ComponentBroadcastDynamic); sendBroadcast(intent); } }); findViewById(R.id.unregister_broadcast).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG, ComponentBroadcastDynamic unregisterReceiver); try{ unregisterReceiver(componentBroadcastDynamic); }catch (Exception e){ Log.e(TAG, ComponentBroadcastDynamic has already unregister); } } }); findViewById(R.id.contentprovider_insert).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String authorities = cn.edu.zafu.component.ComponentContentProvider; Uri CONTENT_URI = Uri.parse(content:// + authorities + /test); ContentValues values = new ContentValues(); values.put(title, title11111111); Uri uri =MainActivity.this.getContentResolver().insert(CONTENT_URI, values); } }); findViewById(R.id.contentprovider_delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String authorities = cn.edu.zafu.component.ComponentContentProvider; Uri CONTENT_URI = Uri.parse(content:// + authorities + /test); int result =MainActivity.this.getContentResolver().delete(CONTENT_URI, title=?, new String[]{title11111111}); Log.e(TAG,=====+result); } }); findViewById(R.id.contentprovider_update).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String authorities = cn.edu.zafu.component.ComponentContentProvider; Uri CONTENT_URI = Uri.parse(content:// + authorities + /test); ContentValues values = new ContentValues(); values.put(title, title11111111); int result =MainActivity.this.getContentResolver().update(CONTENT_URI, values, id=?, new String[]{1}); Log.e(TAG, ===== + result); } }); findViewById(R.id.contentprovider_query).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG,no impl); } }); }}
Follow the Android plug-in-Development-OpenAtlas plug-in adaptation in this article. Add flavors to build. gradle to allow them to run independently and plug-ins at the same time.
productFlavors { alone{ } openatlas { versionName 1.00x23 } }
In this case, we choose to run in the plug-in mode,
After running successfully, click the button from top to bottom,
View log output after running.
You can see that all functions can run normally when running separately. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + 1tC1xMn5w/e4tNbGtb3L3tb31tCjrLKi1/bKyrWx0N64xKGjPC9wPg0KPHByZSBjbGFzcz0 = "brush: java;"> Data-snippet-id = ext.62ad36c5281b478991281e615e074509 data-snippet-saved = false data-codota-status = done>
From the above declaration, we can see that our provider Declaration has changed, but the reason is also very simple. Here we have made a bridge.
The Provider needs to do some special processing, because ContentProvider initializes before Application onCreate, so it makes a bridge to tell the system that this ContentProvider can be used after Initialization is complete. In fact, it has not been completed yet, it is just an empty implementation. it instantiates a normal class when the required class can be loaded.
So what is the implementation of this bridge? It is also very simple. inherit the ProviderProxy class and call the parent class constructor. The parameter passed by calling the constructor is the full Class Name of the real Provider. That is, the class name in the configuration file of the original plug-in.
public class ComponentProviderBridge extends ProviderProxy{ public ComponentProviderBridge() { super(cn.edu.zafu.component.ComponentContentProvider); }}
The Class Name of the inventory file in the host changes to the full Class Name of the bridge. After completing the above work, you can change the build method to openAtlas ..
After the plug-in is generated, a list of plug-in information is also generated, which can be generated according to the previous article.
Then, call the plug-in entry Activity in the host.
findViewById(R.id.component).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClassName(MainActivity.this, cn.edu.zafu.component.MainActivity); startActivity(intent); } });
After running the plug-in, click the corresponding button to enter the plug-in, and click all the buttons from top to bottom to test the function. View log output.
We will find that it is exactly the same as running it separately.
Finally, you need to pay attention to the details. If you use reflection to call the Fragment in the plug-in separately in the host, if you do not pass the four components of the plug-in, such as Activity, the Application of the plug-in will not be called. Here we provide a solution, that is, to manually start the Bundle and call it before reflection. The Code is as follows:
BundleImpl bundle = (BundleImpl)Atlas.getInstance().getBundle(xxxxxxxx);bundle.startBundle();