APP间跳转
APP内部跳转
Intent intent = new Intent();
ComponentName component = new ComponentName("com.example.Activity.", "com.example.Bctivity");
intent.setComponent(component);
startActivity(intent);
APP间跳转
被调用者APP Java代码:
被调用者项目包名:com.example.twodome
被调用Activity 类名: com.example.twodome.MainActivity
package com.example.twodome;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
getData();
}
private void getData() {
// TODO Auto-generated method stub
Intent intent = getIntent();
String name = intent.getStringExtra("username");
String pass = intent.getStringExtra("password");
//if (!"".equals(name)&&!"".equals(pass)) {
if (name!=null&&pass!=null) {
tv.setText(name+" "+pass);
}
}
}
调用者APP Java代码:
调用者项目包名:com.yingzhang.onedomes
调用Activity 类名: com.example.onedome.MainActivity
(一) 如果你不知道那个APP的Activity,但是知道包名(package name),那么可以使用如下的方法:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.twodome");
startActivity(LaunchIntent);
(二) 知晓包名和需要访问的某一个Activity
设置三种方式访问三方APP
//① setClassName
Intent intent = new Intent();// 获取com.example.zzz_b的Context
Context context;
try {
// 要调用另一个APP的activity所在的包名
context = createPackageContext("com.example.twodome", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);
intent.setClassName(context, "com.example.twodome.MainActivity");
//要调用另一个APP的activity名字
Bundle bundle = new Bundle();
bundle.putString("username", "admins111");
bundle.putString("password", "123456");
intent.putExtras(bundle);
startActivity(intent);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
② ComponentName
//Intent intent = new Intent();//Context context;
//try {
// //要调用另一个APP的activity所在的包名
//context = createPackageContext("com.example.twodome", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);
//要调用另一个APP的activity名字
//ComponentName component = new ComponentName(context, "com.example.twodome.MainActivity");
ComponentName component = new ComponentName(context, "com.yingzhang.Activity.PhoneActivity");
//intent.setComponent(component);
//
//intent.putExtra("username", "adminssss");
//intent.putExtra("password", "123456");
//startActivity(intent);
//} catch (NameNotFoundException e) {
TODO Auto-generated catch block
//e.printStackTrace();
//}
③PackageManager
//Bundle bundle = new Bundle();//bundle.putString("username", "admin");
//bundle.putString("password", "123456");
//runPackage(MainActivity.this, "com.example.twodome", bundle);
// ③ runPackage 方法
private void runPackage(Activity activity, String packageName, Bundle bundle) {
PackageManager pm = activity.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setPackage(packageName);
intent.putExtras(bundle);
List<ResolveInfo> infoList = pm.queryIntentActivities(intent, 0);
if (infoList != null && infoList.size() > 0) {
ResolveInfo info = infoList.get(0);
String className = info.activityInfo.name;
ComponentName component = new ComponentName(packageName, className);
intent.setComponent(component);
activity.startActivity(intent);
}
}
回调
跳转到App之后也可以回调回来 startActivityForResult(intent, REQUEST_CODE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE) {
if (data==null) {
return;
}
String res = data.getStringExtra("result");
Log.i("ddddddd",res);
if (res.equals("支付成功")||res.equals("订单已经支付")) {
payTypes = "已付款";
updateData();
Toast.makeText(ConfirmOrderActivity.this, res,Toast.LENGTH_LONG).show();
} else {
payTypes = "待付款";
updateData();
}
} else
super.onActivityResult(requestCode, resultCode, data);
}