Android中一个APP启动另一个APP并传递参数

时间:2021-06-30 16:45:15

被调用(启动)的APP:
项目名字:Demo_ybs
项目包名:com.ybs.demo_ybs
被调用APP中获取调用者的传递数据:

package com.ybs.demo_ybs;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

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

getAppData();
}

private void getAppData() {

Intent intent=getIntent();
if(intent.getFlags()==101){
String data_str = intent.getStringExtra("data");
Log.i("tag", data_str);
//tag: 调用者传递过来的数据

}

}



}

调用者APP:
项目名字:Demo1
项目包名:com.ybs.ymt
调用者APP中传递数据:

package com.ybs.ymt;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;

public class MainActivity extends Activity {
private String reqData;

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

}

private String setData() {
reqData = "调用者传递过来的数据";
return reqData;
}
//点击按钮后触发调用另一个APP
public void onClcik(View view) {
//要调用另一个APP的activity所在的包名
String packageName = "com.ybs.demo_ybs";
//要调用另一个APP的activity名字
String activity = "com.ybs.demo_ybs.MainActivity";
ComponentName component = new ComponentName(packageName, activity);
Intent intent = new Intent();
intent.setComponent(component);
intent.setFlags(101);
intent.putExtra("data", setData());
startActivity(intent);
}

}

方法2:
被调用(启动)的APP:
项目名字:Demo_ybs
项目包名:com.ybs.demo_ybs
被调用APP中获取调用者的传递数据:

package com.ybs.demo_ybs;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;



public class MainActivity extends Activity {

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

getAppData();
}

private void getAppData() {

Intent intent=getIntent();
if(intent.getFlags()==101){
//String data_str = intent.getStringExtra("data");
Bundle bundle=intent.getExtras();
String data_str = bundle.getString("data");
Log.i("tag", data_str);

}

}

//点击按钮触发此事件
public void onClick(View view){
Intent data = new Intent();
//参数1:要调用另一个APP的activity所在的包名
//参数2:要调用另一个APP的activity名字
data.setClassName("com.ybs.ymt", "com.ybs.ymt.MainActivity");
data.putExtra("return_data", "回传给调用者的数据");
setResult(10, data); //设置返回数据
this.finish(); //关闭当前Activity
Log.i("tag", "回传给调用者的数据");
}


}

调用者APP:
项目名字:Demo1
项目包名:com.ybs.ymt
调用者APP中传递数据:

package com.ybs.ymt;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.KeyguardManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;

public class MainActivity extends Activity {
private String reqData;
private int IMPORTANCE_FOREGROUND=100;
private int IMPORTANCE_VISIBLE=130;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}




public void onClcik(View view) {


/*
//此方法也可以去启动另一个APP
Intent intent = new Intent();

intent.setComponent(new ComponentName("com.ybs.demo_ybs", "com.ybs.demo_ybs.MainActivity"));

if (!isBackgroundRunning()) {
intent.setAction("android.intent.action.MAIN");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

intent.putExtra("data", "调用者传递过来的数据aaa");

startActivity(intent);*/


Intent intent = new Intent();
intent.setFlags(101);
Bundle bundle = new Bundle();
bundle.putString("data", "调用者传递过来的数据aaa");
intent.putExtras(bundle);
//参数1:要调用另一个APP的activity所在的包名
//参数2:要调用另一个APP的activity名字
intent.setClassName("com.ybs.demo_ybs", "com.ybs.demo_ybs.MainActivity");
startActivityForResult(intent, 1);



}





//在启动前进行一次判断:app是否在后台运行。一下是判断app是否运行的方法:
private boolean isBackgroundRunning() {
String processName = "com.ybs.demo_ybs";
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (activityManager == null)
return false;
// get running application processes
List<ActivityManager.RunningAppProcessInfo> processList = activityManager
.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processList) {
/*
BACKGROUND=400 EMPTY=500 FOREGROUND=100
GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
*/

if (process.processName.startsWith(processName)) {
boolean isBackground = process.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& process.importance != RunningAppProcessInfo.IMPORTANCE_VISIBLE;
boolean isLockedState = keyguardManager
.inKeyguardRestrictedInputMode();
if (isBackground || isLockedState)
return true;
else
return false;
}
}
return false;
}


//此方法中可以接收另一个app回传回来的数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("tag", "onActivityResult1");
if(resultCode == 10 && requestCode == 1){
Log.i("tag", "onActivityResult2");

String return_data=data.getStringExtra("return_data");

Log.i("tag", "调用者返回的数据:" + return_data);

}
super.onActivityResult(requestCode, resultCode, data);
}





}