app间跳转传参【iOS android】

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sunzhenglin2016/article/details/85124942

iOS
A->B
A: 1.在iOS9中,如果使用 canOpenURL:方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是对应应用程序的URL Schemes。(test)
2.代码:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@“test://Page1”]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“test://”] options:nil completionHandler:nil];
}else{
NSLog(@“尚未安装应用”);
//可以跳商店去
}

B:
targets -> Info -> URL Types -> 填写scheme
命名一个名字test
代码:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // 1.获取导航栏控制器
    UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
    // 2.获得主控制器
    ViewController *mainVc = [rootNav.childViewControllers firstObject];

    // 3.每次跳转前必须是在跟控制器(细节)
    [rootNav popToRootViewControllerAnimated:NO];   

    // 4.根据字符串关键字来跳转到不同页面
    if ([url.absoluteString containsString:@"Page1"]) { // 跳转到应用App-B的Page1页面
        // 根据segue标示进行跳转
        [mainVc performSegueWithIdentifier:@"homeToPage1" sender:nil];
    } else if ([url.absoluteString containsString:@"Page2"]) { // 跳转到应用App-B的Page2页面
        // 根据segue标示进行跳转
        [mainVc performSegueWithIdentifier:@"homeToPage2" sender:nil];
    }   

    return YES;

android

A:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              if (checkApkExist(MainActivity.this,"free.com.bproject")){
                    Intent intent = new Intent();
                    intent.setClassName("free.com.bproject", "free.com.bproject.MainActivity");
                    Bundle bundle = new Bundle();
                    bundle.putString("FIRST_APP_KEY", "this is a app send message");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this,"应用未安装",Toast.LENGTH_SHORT).show();

                }




            }
        });
    }
    public static boolean checkApkExist(Context context, String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);

            return true;
        } catch (PackageManager.NameNotFoundException e) {

            return false;
        }
    }

B:

  <activity
            android:name=".MainActivity"
            android:exported="true"
            >

 Bundle bundle = this.getIntent().getExtras();
    if (bundle != null){ //防止直接启动MainActivity时空指针闪退
        String value = bundle.getString("FIRST_APP_KEY");
        TextView textView = findViewById(R.id.textview);
        textView.setText(value);
    }
展开阅读全文

没有更多推荐了,返回首页