两个android程序间的相互调用传递参数

来源:互联网 发布:Windows 7定时删除文件 编辑:程序博客网 时间:2024/05/22 21:53

两个App之间相互切换调用

本文部分转载来自:http://blog.csdn.net/zygang1988/article/details/7733541

  • App之间跳转传递参数
  • 需要注意事项

App之间跳转传递参数

假设现在有两个App暂时命名为A,B两个应用,A如果要跳转到B应用当中需要配置一下代码:(本文简单创建两个简单应用,如果需要看效果图,请查看转载的网页)

应用A中你所需跳转的:

 /**   * @param v 设置按钮点击事件   */    @Override    public void onClick(View v) {        super.onClick(v);        switch (v.getId()) {            case R.id.tv_content://跳转点击事件            //ComponentName 做跳转,第一个参数传递是B应用的包名,第二个参数传递的是你所需要跳转到B应用的界面                try {                    ComponentName componentName = new ComponentName("com.example.intentActivity2", "com.example.intentActivity2.SecondActivity");                    Intent intent = new Intent();                  //  Intent intent = new Intent("chroya.foo");                    Bundle bundle = new Bundle();                    bundle.putString("args", "我就是跳转过来的");                    intent.putExtras(bundle);                    intent.setComponent(componentName);                    startActivity(intent);                } catch (ActivityNotFoundException e) {                //判断是否安装B应用,提供下载链接                    NameToast.show(mContext, "请下载----" + "com.example.intentActivity2");                    e.printStackTrace();                }                break;        }    }

应用B中所需配置:

在应用B中我创建了两个类,一个是应用类的入口activity,一个是自定义的activity,我会在下面做出说明和区别:

package com.example.MainActivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;/** * 项目名称:intentActivity2 * 类描述: * 创建人:许仙仙 * 创建时间:2015/10/16 16:34 * 修改人:许仙仙 * 修改时间:2015/10/16 16:34 * 修改备注: */public class MainActivity extends Activity {    private TextView tv_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        this.tv_content = (TextView) this.findViewById(R.id.tv_content);        Intent intent = getIntent();        String value = intent.getStringExtra("args");        if (value != null && !value.equals("")) {            tv_content.setText("应用入口"+value);//这里将显示“这是跳转过来的!来自apkA”        } else {            tv_content.setText("空的参数");        }    }}
package com.example.intentActivity2;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;/** * 项目名称:intentActivity2 * 类描述: * 创建人:许仙仙 * 创建时间:2015/10/16 16:34 * 修改人:许仙仙 * 修改时间:2015/10/16 16:34 * 修改备注: */public class SecondActivity extends Activity {    private TextView tv_content;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        this.tv_content = (TextView) this.findViewById(R.id.tv_content);        Intent intent = getIntent();        String value = intent.getStringExtra("args");        if (value != null && !value.equals("")) {            tv_content.setText("自定义activity"+value);//这里将显示“这是跳转过来的!来自apkA”        } else {            tv_content.setText("空的参数");        }    }}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.intentActivity2">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme">        <activity            android:name="com.example.intentActivity2.MainActivity"            android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name="com.example.intentActivity2.SecondActivity"            android:exported="true">            <action android:name="chroya.foo" />            <category android:name="android.intent.category.DEFAULT" />        </activity>    </application></manifest>

大家看到上文A应用中有一段Intent中是加入了参数的:
方法1: Intent intent = new Intent();
方法2:Intent intent = new Intent(“chroya.foo”);
方法1中没有传入参数,一般是调用B应用入口类
方法2则是给Activity界面给一个自定义命名

注意事项

方法二中,在清单文件中我们需要配置这个自定义命名类一个属性android:exported=”true”,如果没有配置调用的时候会出现ActivityNotFoundException异常,表示没有找到相关类。代码量比较小,我就不上传源码了。如果哪里写的有问题,还请大家一起指正。

转载时请命名出处:http://blog.csdn.net/lvlelygirl/article/details/49182365

0 0
原创粉丝点击