android四个组件的跨进程通信
Android四大組件(Activity,service,broadcast,Content Provider)跨進程通信相信在android項目中進程用到,此處將一一做以說明以及總結.
1.簡括:
首先需要看四大組件都繼承了什么,實現了什么,也就知道了它們的用途以及作用.
?
public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2,Window.Callback, KeyEvent.Callback,OnCreateContextMenuListener, ComponentCallbacks2,Window.OnWindowDismissedCallback, WindowControllerCallback,AutofillManager.AutofillClientpublic abstract class Service extends ContextWrapper implements ComponentCallbacks2 *****public class ContextThemeWrapper extends ContextWrapper從activity和service看出來,它們都繼承自context實現不同的接口,實現決定了它們的功能.?
?
?
2.Activity:界面顯示組件,且實現了很多Ui交互接口.
(1)不同app跨進程調用主要通過startActivity. eg:OneMainActivity調用AnotherMainActivity.下面只做簡單說明.需要運行驗證的可以copy代碼進行運行校驗.
package com.example.oneactivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button;public class OneMainActivity extends Activity {Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_one_main);button = (Button)findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction("com.ysfl.otherActivity");//此處設置需要起動app的action.Log.d(this.getClass().getName(),"I'm OneMainActivity onclick to start anotherActivity");startActivity(intent);//跨進程調用其它應用}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_one_main, menu);return true;}@Overrideprotected void onPause(){super.onPause();Log.d(this.getClass().getName(),"I'm OneMainActivity onPause");}@Overrideprotected void onStop(){super.onStop();Log.d(this.getClass().getName(),"I'm OneMainActivity onStop");}@Overrideprotected void onDestroy(){super.onDestroy();Log.d(this.getClass().getName(),"I'm OneMainActivity onDestroy");} }
?OneMainActivity的xml文件不做說明,它只是一個簡單的配置.AnotherMainActivity的代碼和配置文件如下.
package com.example.anotheractivity;import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.widget.Toast;public class AnotherMainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_another_main);Log.d(this.getClass().getName(),"I'm AnotherMainActivity onCreate");}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_another_main, menu);return true;}@Overrideprotected void onPause(){super.onPause();Log.d(this.getClass().getName(),"I'm AnotherMainActivity onPause");}@Overrideprotected void onStop(){super.onStop();Log.d(this.getClass().getName(),"I'm AnotherMainActivity onStop");}@Overrideprotected void onDestroy(){super.onDestroy();Log.d(this.getClass().getName(),"I'm AnotherMainActivity onDestroy");} } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.anotheractivity"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.anotheractivity.AnotherMainActivity"android:label="@string/app_name" ><intent-filter><!-- 此處的action是其它應用需要起動的action --><action android:name="com.ysfl.otherActivity" /><category android:name="android.intent.category.DEFAULT" /><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application> </manifest>
運行結果如下圖:
?
3.Service
(1)activity到service跨進程有兩種方式:startService和bindService
?? a.startService單向傳遞數據
?? b.bindService可通過binder實現數據的交互以及監聽。
(2)service生命周期如下:
startService:onCreate()->onStartCommand()-----service running -----onDestroy()
bindService:onCreate()->onBind()----------service running ?-------onUnbind()->onDestroy()
service總結:當service已經被oncreate后,就不會再次oncreate,因為service只有一個,不會被重復創建.當被bind后也不會進行再次bind,此時binder已經存在于緩存區.
(3)關于activity跨進程調用service的例子可以直接參考:Android進程間通信-AIDL-經典的Hello World詮釋
后續待續..........
?
轉載于:https://www.cnblogs.com/syyh2006/p/9187676.html
總結
以上是生活随笔為你收集整理的android四个组件的跨进程通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库MySQL/mariadb知识点—
- 下一篇: 百度地图API功能