【Android】App与App之间的交互

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_34578833/article/details/83119431


今天刚好公司有一个Android无界面插件化的打印App需求,随便在这里写下一点心得

知识点分析

  1. 无界面
  2. 可比本公司的其他App任意调用
  3. 隐藏App的启动图标

一. 插件App

(一)修改activity的清单文件

  1. 先创建一个插件项目并创建一个activity
  2. 让它集成AppCompatActivity的祖宗Activity
  3. 然后在修改activity的theme
android:theme="@android:style/Theme.NoDisplay"
  1. 然后在清单文件对应的activity标签下的intent-filter标签中自定义一个action
<action android:name="com.chy.local" />
  1. 最后把该intent-filter标签中的 category标签的内容改为

完整的代码如下

<activity
           android:name=".MainActivity"
           android:theme="@android:style/Theme.NoDisplay">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <action android:name="com.chy.local" />
               <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>
       </activity>

(二)对插件activity下手

public class PrintActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Intent intent = this.getIntent();
        final Bundle bundle = intent.getBundleExtra("content");
        if (bundle != null) {
            toast(bundle.getString("str"));
        } else {
            toast("没有接收到参数");
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                bundle.putString("str", "回调给你");
                intent.putExtra("content", bundle);
                setResult(1, intent);
                toast("开始回调");
                finish();
            }
        }, 10000);
    }

    private void toast(String string) {
        Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }
}

(三)打包这个插件并安装到手机上面,安装完成之后只能在系统的应用管理界面才能找到

重点是在插件的回调中,一定要先 setResult(int resultCode, Intent data)的后面马上调用finish(),而且不能在其之前调用finish()

二.调用App

(一)顺其自然的创建一个新的项目,并顺其自然的创建一个activity

(二)直接上代码

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
private String TAG="MainActivity ";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        Intent intent = new Intent();
        intent.setAction("com.chy.local");
        Bundle bundle = new Bundle();
        bundle.putString("str", "调用成功了");
        intent.putExtra("content", bundle);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle bundle = data.getBundleExtra("content");
        Log.e(TAG, "requestCode:" + requestCode);
        Log.e(TAG, "resultCode:" + resultCode);
        Log.e(TAG, "forResult:" + bundle.getString("str"));
    }
}

结果

结果

重点

如果要跨app通讯需要使用到Serializable的,则需要如下操作

  1. 在插件app中的bean对象implements Serializable,并生成对应的serialVersionUID值,例如:
public class PersonBean implements Serializable {
    private static final long serialVersionUID = -4900450649994542348L;
    public int id;
    public String name;
    public int age
}
  1. 复制插件app中的这个bean对象所在的包路径到调用app中创建这个包路径,并把插件中的bean复制到调用 app 同样的包路径下,意思就是说,两个项目中的那个bean需要有同样的包名,并且serialVersionUID需要一致
  2. 调用app的Intent值使用下面这个方法时
    setClassName(@NonNull String packageName, @NonNull String className)
    packageName为对应插件app中的包名,className为对应插件app中被调用的包路径+类名,具体使用方法如下:
    例如,bean对象为PersonBean,插件app的packageNamecom.chy.testclassNamecom.chy.test.TestActivity
PersonBean personBean=new PersonBean();
Bundle bundle = new Bundle();
bundle.putSerializable("person", personBean);
//打开app
Intent intent = new Intent();
intent.setClassName("com.chy.test","com.chy.test.TestActivity");
intent.putExtras(bundle);
startActivity(intent);
  1. 插件app接收到数据的时候,就像往常一样通过bundle取出来就可以使用了
  2. 总的来说,跨app通讯要通过序列化穿参数,就要注意两点
    a. serialVersionUID的值需要一致
    b. intent跳转时,setClassname的两个参数一定要正确

猜你喜欢

转载自blog.csdn.net/qq_34578833/article/details/83119431