表現(xiàn)方式大致如下:
1.Android App A(下面簡(jiǎn)稱為 AppA)喚起 Android App B(下面簡(jiǎn)稱為 AppB)
2.AppA 喚起 AppB 的時(shí)候,將一些數(shù)據(jù)帶給 AppB
3.AppB 處理數(shù)據(jù),點(diǎn)擊按鈕關(guān)閉 AppB, 同時(shí)跳轉(zhuǎn)回 AppA,并帶回處理后的數(shù)據(jù)
實(shí)現(xiàn)方式大致如下:
1.AppA 使用固定數(shù)據(jù)(AppB的包名、AppB接收跳轉(zhuǎn)的Activity名),通過(guò) startActivityForResult 方法喚起 AppB 的 Activity。
2.AppA 喚起 AppB 的時(shí)候,將一些數(shù)據(jù)放在 Bundle 中帶給 AppB
3.AppB 處理數(shù)據(jù),并通過(guò) setResult 方法保存數(shù)據(jù),點(diǎn)擊按鈕關(guān)閉 AppB (也就是 Activity 的 finish()), 關(guān)閉了 AppB 后會(huì)自動(dòng)跳轉(zhuǎn)回 AppA,并帶回處理后的數(shù)據(jù)
4.【關(guān)鍵】AppA 通過(guò)重寫 onActivityResult 方法,就可以接收到 AppB setResult 后的數(shù)據(jù)
AppA 通過(guò)這種方式喚起 AppB,在跳轉(zhuǎn)回來(lái)的時(shí)候,AppB 并不需要知道 AppA 的包名和 Activity 名,而且可以將值帶回 AppA
下面直接上代碼:
AppA:###
///接收跳轉(zhuǎn)回來(lái)的參數(shù)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
//這個(gè) requestCode 是在喚起 AppB 的時(shí)候傳進(jìn)
// startActivityForResult 方法里的
//用于判斷是哪個(gè)功能喚起的 AppB
if (resultCode == RESULT_OK) { //在這里接收
String result = data.getStringExtra("data");
UnityPlayer.UnitySendMessage("NativePlatform", "OnBBHLoginReturn", result);
}
}
}
///跳轉(zhuǎn)到 AppB ,并將自定義的參數(shù)傳過(guò)去
public void JumpForLogin(final String mKey){
ComponentName componetName = new ComponentName(
"com.test.demo",
"com.test.demo.TestActivit");
Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse(bbhLoginUrl));
Bundle bundle = new Bundle();
bundle.putString("keyNumber", mKey);
intent.putExtras(bundle);
startActivityForResult(intent,1); //這個(gè) 1 就是上面的 requestCode (請(qǐng)求碼),需要大于0
}
AppB: (包名 "com.test.demo",Activit 名 "TestActivit")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得 AppA 傳過(guò)來(lái)的參數(shù)并顯示
Intent intent= getIntent();
String value = intent.getStringExtra("mkey");
TextView lblTitle=(TextView)findViewById(R.id.lblTitle);
lblTitle.setText("keyNumber: " + value);
}
//點(diǎn)擊按鈕
public void click(View v){
Intent intent = new Intent();
intent.putExtra("data", "{\"userid\":123456,\"token\":\"sHdqncsSDf\"}");
setResult(RESULT_OK,intent);//保存數(shù)據(jù)
finish();//結(jié)束當(dāng)前 Activity
}