?一、启动其他程序代码
Intent intent = new Intent();
intent.setClassName(packageName, activityName); // 指定应用包名和activity名
intent.putExtra("stringData", "我传送的内容");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // 正常还要检测是否安装这个程序
二、接收参数代码
Intent intent = getIntent();
// String data = intent.getStringExtra("patientName"); // 获取指定字段
Bundle bundle = intent.getExtras(); // 获取所有参数
String name=bundle.getString("patientName");
if(name!=null){
Log.e("唤醒参数1", name);
}
所有代码
public class MainActivity extends AppCompatActivity {
String packageName="com.xxx"; // 你要启动包名
String activityName="com.xxx.MainActivity"; // 指定到具体的activity
public Context activityCont;
String SelfTag="Demo————————————————————";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityCont=this;
// 检测接收参数
Intent intent = getIntent();
// String data = intent.getStringExtra("patientName"); // 获取指定字段
Bundle bundle = intent.getExtras(); // 获取所有参数
String name=bundle.getString("patientName");
if(name!=null){
Log.e("唤醒参数1", name);
}
// register click event
Button button = (Button) this.findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isAppInstalled(activityCont,packageName)){
startTDMS();
}else{
Toast.makeText(activityCont, "应用程序未安装", Toast.LENGTH_SHORT).show();
Log.i(SelfTag,"应用程序未安装");
}
}
});
}
// to start Applications
public void startTDMS(){
Intent intent = new Intent();
intent.setClassName(packageName, activityName);
intent.putExtra("stringData", "我传送的内容");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
// check Applications is installed
private boolean isAppInstalled(Context context, String pgName){
PackageInfo packageInfo;
try {
packageInfo = context.getPackageManager().getPackageInfo(pgName, 0);
}catch (PackageManager.NameNotFoundException e) {
packageInfo = null;
// e.printStackTrace();
}
return packageInfo!=null;
}
// other way,you can ignore
public void sameWayStartTDMS(){
Intent intent = new Intent();
ComponentName comp = new ComponentName(packageName, activityName);
intent.setComponent(comp);
int num = (int) ((Math.random() * 9 + 1) * 10);
intent.putExtra("jsonData","NowRangeNumberIs_"+ num);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
另:如果检测是否安装应用一直提示未安装,实际已安装,那测试手机可能是安卓11了,检测程序要加个权限
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
|