App中启动另一个App

启动appA中增加的代码
public class MainActivity extends Activity {
    private Button btn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View arg0) {
                //com.ustcinfo.ict.hbhxapp 被启动app的包名
//Intent in = getPackageManager().getLaunchIntentForPackage("com.ustcinfo.ict.ahhxapp");
                Intent in = new Intent();
//com.ustcinfo.ict.ahhxapp 被启动包名;com.ustcinfo.ict.platform.ui.LoginActivity  被启动指定类全名
                in.setClassName("com.ustcinfo.ict.ahhxapp", "com.ustcinfo.ict.platform.ui.LoginActivity");
                if(in !=null){
                    in.putExtra("str1", "value1");  // 要传递给 app B的参数
                    in.putExtra("str2", "value2");
                    in.putExtra("str3", "value3");
                    startActivity(in); //启动app B
                }
            }
            
        });
    }
}


被启动app B增加代码
被启动指定的Activity 在AndroidMenifest.xml中要暴露入口,以便app 能够启动,不暴露入口,会报权限错误,不能正常启动
被启动的Activity暴露入口方式,android:exported="true" ,默认值是false

 <activity android:name="com.ustcinfo.ict.platform.ui.LoginActivity" android:exported="true">
 </activity>

获取app A传递过来的参数:

String str1 =getIntent().getStringExtra("str1"); //获取app A传递的参数


String str2=getIntent().getStringExtra("str2"); //获取app A传递的参数


String str3=getIntent().getStringExtra("str3"); //获取app A传递的参数

猜你喜欢

转载自blog.csdn.net/grandgrandpa/article/details/83185700