android 進程間通信Activity

Activity跨進程調用其實很常見,比如在第三方sdk中的分享,授權登錄等,如果我們有多個應用包相互調用共享activity 是要用到的。

1.新建項目,用於被調用 主activity 爲 ReceiveActivity

2.註冊activity,並且設置action

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ReceiveActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

                <!--自定義action 用於可以跨進程被調用-->
                <action android:name="com.test.MainActivity"/>
                <!--自定義action 要添加-->
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

特別注意,這裏的action 用於調用該activity時的標籤

3.ReceiveActivity 內代碼實現

public class ReceiveActivity extends AppCompatActivity {
    private static final String TAG = "ReceiveActivity";
   
    private TextView mTest;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        initView();
      
    }

    private void initView() {
        mTest = (TextView) findViewById(R.id.test);
        Intent intent=getIntent();
        if (intent!=null) {
            String tag=intent.getStringExtra("tag");
            if (tag!=null) {
                mTest.setText("我收到了:"+tag);
            }
        }
    }
    
}

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveActivity">

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

被調用端完畢

2.新建項目,用於調用ReceiveActivity 進程通訊

設置action並且傳遞參數
Intent intent = new Intent();
intent.setAction("com.test.ReceiveActivity");
intent.putExtra("tag", "調用成功了");
startActivity(intent);

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 開始調用另一個進程
     */
    private TextView mSend;
  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mSend = (TextView) findViewById(R.id.send);
        mSend.setOnClickListener(this);
      
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.send:
                Intent intent = new Intent();
                intent.setAction("com.test.MainActivity");
                intent.putExtra("tag", "調用成功了");
                startActivity(intent);
                break;
         
        }
    }
}

 

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="開始調用另一個進程"
      />
   

</LinearLayout>

將兩個項目都安裝到設備上,這時點擊“開始調用另一個進程” 調起ReceiveActivity 並將傳遞的參數顯示出來。

這裏會發現啓完成後查看進程,只有一個(先殺掉所有進程再啓動),這是因爲ReceiveActivity 使用的是默認的啓動模式,重新new了一個ReceiveActivity 並且放到了調用項目的棧中,這樣就達不到共享的目的,並且這時你打開被調用項目,發現不是剛纔的那個了,又新建了一個ReceiveActivity 類,且相同類不在一個進程中。這裏改下啓動模式就好了

設置ReceiveActivity  啓動模式

android:launchMode="singleInstance"

就這麼多了,結束。

 

源碼下載地址

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章