{"id":1227,"date":"2023-03-18T09:14:17","date_gmt":"2023-03-18T01:14:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1227"},"modified":"2023-04-29T09:36:06","modified_gmt":"2023-04-29T01:36:06","slug":"communication-methods-between-atlas-plugin-bundles-remotetransactor-remoteview-and-remotefragment","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/communication-methods-between-atlas-plugin-bundles-remotetransactor-remoteview-and-remotefragment\/","title":{"rendered":"Atlas\u63d2\u4ef6Bundle\u95f4\u7684\u901a\u4fe1\u65b9\u5f0fRemoteTransactor\u3001RemoteView\u3001RemoteFragment"},"content":{"rendered":"<p>Atlas\u63d0\u4f9b\u4e863\u79cdBundle\u4e0eBundle\u4e4b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\uff1aRemoteTransactor\u3001RemoteView\u3001RemoteFragment\u3002RemoteTransactor\u662fRemoteView\u3001RemoteFragment\u901a\u8baf\u65b9\u5f0f\u7684\u7b80\u5316\u7248\uff0c\u4ec5\u4ec5\u4e3a\u4e86Bundle\u548cBundle\u4e4b\u95f4\u7684\u901a\u8baf\u800c\u5b58\u5728\u3002<\/p>\n<p>\u4ee5firstbundle\u8c03\u7528secondbundle\u4e3a\u4f8b<\/p>\n<p><!-- more --><\/p>\n<h2>RemoteView<\/h2>\n<h3>\u5728secondbundle\u4e2d\u5b9a\u4e49View\u5b9e\u73b0IRemote<\/h3>\n<p>MyView.java<\/p>\n<pre><code class=\"language-java\">package cn.appblog.secondbundle;\n\nimport android.content.Context;\nimport android.support.annotation.Nullable;\nimport android.util.AttributeSet;\nimport android.widget.TextView;\n\npublic class MyView extends TextView {\n    public MyView(Context context) {\n        super(context);\n    }\n\n    public MyView(Context context, @Nullable AttributeSet attrs) {\n        super(context, attrs);\n    }\n\n    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n    }\n}<\/code><\/pre>\n<p>MyFrameLayout.java<\/p>\n<pre><code class=\"language-java\">package cn.appblog.secondbundle;\n\nimport android.app.ProgressDialog;\nimport android.content.Context;\nimport android.os.Bundle;\nimport android.support.annotation.AttrRes;\nimport android.support.annotation.NonNull;\nimport android.support.annotation.Nullable;\nimport android.support.annotation.StyleRes;\nimport android.taobao.atlas.remote.IRemote;\nimport android.util.AttributeSet;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.widget.FrameLayout;\n\npublic class MyFrameLayout extends FrameLayout implements IRemote {\n    public MyFrameLayout(@NonNull Context context) {\n        super(context);\n        init();\n    }\n\n    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {\n        super(context, attrs);\n        init();\n    }\n\n    public MyFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        init();\n    }\n\n    public void init() {\n        LayoutInflater inflater = LayoutInflater.from(getContext());\n        inflater.inflate(R.layout.layout_test, this);\n        this.findViewById(R.id.home_page).setOnClickListener(new OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                ProgressDialog dialog = new ProgressDialog(getContext());\n                dialog.show();\n            }\n        });\n    }\n\n    @Override\n    public Bundle call(String commandName, Bundle args, IResponse callback) {\n        return null;\n    }\n\n    @Override\n    public &lt;T&gt; T getRemoteInterface(Class&lt;T&gt; interfaceClass, Bundle args) {\n        return null;\n    }\n}<\/code><\/pre>\n<p>layout_test.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:orientation=&quot;vertical&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n\n    &lt;cn.appblog.secondbundle.MyView\n        android:id=&quot;@+id\/home_page&quot;\n        android:layout_width=&quot;100dp&quot;\n        android:layout_height=&quot;100dp&quot;\n        android:textSize=&quot;18sp&quot;\n        android:text=&quot;http:\/\/www.appblog.cn&quot;\n        \/&gt;\n\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<h3>\u5728secondbundle\u4e2d\u6ce8\u518cView<\/h3>\n<p>\u5728secondbundle\u7684AndroidManifest.xml\u4e2d\u6ce8\u518cMyFrameLayout\uff08application\u6807\u7b7e\u4e0b\uff09<\/p>\n<pre><code class=\"language-xml\">&lt;meta-data android:name=&quot;atlas.view.intent.action.SECOND_VIEW&quot; android:value=&quot;cn.appblog.secondbundle.MyFrameLayout&quot;\/&gt;<\/code><\/pre>\n<h3>\u5728firstbundle\u4e2d\u8c03\u7528View<\/h3>\n<pre><code class=\"language-java\">RemoteFactory.requestRemote(RemoteView.class, MainActivity.this, new Intent(&quot;atlas.view.intent.action.SECOND_VIEW&quot;),\n        new RemoteFactory.OnRemoteStateListener&lt;RemoteView&gt;() {\n            @Override\n            public void onRemotePrepared(RemoteView iRemoteContext) {\n                FrameLayout layout = (FrameLayout) findViewById(R.id.layout_content);\n                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);\n                layout.addView(iRemoteContext, params);\n            }\n\n            @Override\n            public void onFailed(String s) {\n                Log.e(&quot;yezhou&quot;, s);\n            }\n        });<\/code><\/pre>\n<h2>RemoteFragment<\/h2>\n<h3>\u5728secondbundle\u4e2d\u5b9a\u4e49Fragment\u5b9e\u73b0IRemote<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.secondbundle;\n\nimport android.os.Bundle;\nimport android.support.annotation.Nullable;\nimport android.support.v4.app.Fragment;\nimport android.taobao.atlas.remote.IRemote;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\n\npublic class MyFragment extends Fragment implements IRemote {\n\n    @Nullable\n    @Override\n    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {\n        View v = inflater.inflate(R.layout.layout_test, container, false);\n        return v;\n    }\n\n    @Override\n    public Bundle call(String s, Bundle bundle, IResponse iResponse) {\n        return null;\n    }\n\n    @Override\n    public &lt;T&gt; T getRemoteInterface(Class&lt;T&gt; aClass, Bundle bundle) {\n        return null;\n    }\n}<\/code><\/pre>\n<h3>\u5728secondbundle\u4e2d\u6ce8\u518cView<\/h3>\n<p>\u5728secondbundle\u7684AndroidManifest.xml\u4e2d\u6ce8\u518cMyFragment\uff08application\u6807\u7b7e\u4e0b\uff09<\/p>\n<pre><code class=\"language-xml\">&lt;meta-data android:name=&quot;atlas.fragment.intent.action.SECOND_FRAGMENT&quot; android:value=&quot;cn.appblog.secondbundle.MyFragment&quot;\/&gt;<\/code><\/pre>\n<h3>\u5728firstbundle\u4e2d\u8c03\u7528View<\/h3>\n<pre><code class=\"language-java\">RemoteFactory.requestRemote(RemoteFragment.class, MainActivity.this, new Intent(&quot;atlas.fragment.intent.action.SECOND_FRAGMENT&quot;),\n        new RemoteFactory.OnRemoteStateListener&lt;RemoteFragment&gt;() {\n            @Override\n            public void onRemotePrepared(RemoteFragment iRemote) {\n                getSupportFragmentManager().beginTransaction().add(R.id.layout_fragment_content, iRemote).commit();\n            }\n\n            @Override\n            public void onFailed(String s) {\n                Log.e(&quot;yezhou&quot;, s);\n            }\n        });<\/code><\/pre>\n<h2>RemoteTransactor<\/h2>\n<h3>\u5728\u516c\u5171module\u4e2d\u5b9a\u4e49\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.middleware;\n\npublic interface ICaculator {\n    int sum(int a,int b);\n}<\/code><\/pre>\n<h3>\u5728secondbundle\u4e2d\u5b9a\u4e49IRemote\u5b9e\u73b0\u7c7b<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.secondbundle;\n\nimport android.content.ComponentCallbacks2;\nimport android.content.ContentProvider;\nimport android.content.res.Configuration;\nimport android.os.Bundle;\nimport android.taobao.atlas.remote.HostTransactor;\nimport android.taobao.atlas.remote.IRemote;\nimport android.taobao.atlas.runtime.ActivityLifeCycleObserver;\nimport android.taobao.atlas.runtime.RuntimeVariables;\n\nimport cn.appblog.middleware.ICaculator;\n\npublic class CaculatorRemote implements IRemote {\n\n    public CaculatorRemote() {\n        RuntimeVariables.androidApplication.registerComponentCallbacks(new ComponentCallbacks2() {\n            @Override\n            public void onTrimMemory(int level) {\n                if (level == ContentProvider.TRIM_MEMORY_UI_HIDDEN) {\n                    HostTransactor remote = HostTransactor.get(CaculatorRemote.this);\n                    if (remote != null) {\n                        remote.call(&quot;SLEEP_NOTIFY&quot;, null, null);\n                    }\n                }\n            }\n\n            @Override\n            public void onConfigurationChanged(Configuration newConfig) {\n\n            }\n\n            @Override\n            public void onLowMemory() {\n\n            }\n        });\n    }\n\n    @Override\n    public Bundle call(String s, Bundle bundle, IResponse iResponse) {\n        if (s.equalsIgnoreCase(&quot;sum&quot;)) {\n            int a = bundle.getInt(&quot;num1&quot;);\n            int b = bundle.getInt(&quot;num2&quot;);\n            bundle.putInt(&quot;result&quot;, a + b);\n            return bundle;\n\n        }\n        return null;\n    }\n\n    @Override\n    public &lt;T&gt; T getRemoteInterface(Class&lt;T&gt; aClass, Bundle bundle) {\n        if (aClass == ICaculator.class) {\n            T instance = (T) new ICaculator() {\n                @Override\n                public int sum(int a, int b) {\n                    return a + b;\n                }\n            };\n            return instance;\n        }\n        return null;\n    }\n}<\/code><\/pre>\n<h3>\u5728secondbundle\u4e2d\u6ce8\u518cIRemote<\/h3>\n<p>\u5728secondbundle\u7684AndroidManifest.xml\u4e2d\u6ce8\u518cMyFragment\uff08application\u6807\u7b7e\u4e0b\uff09<\/p>\n<pre><code class=\"language-xml\">&lt;meta-data android:name=&quot;atlas.transaction.intent.action.SECOND_TRANSACTION&quot; android:value=&quot;cn.appblog.secondbundle.CaculatorRemote&quot;\/&gt;<\/code><\/pre>\n<h3>\u5728firstbundle\u4e2d\u8c03\u7528View<\/h3>\n<pre><code class=\"language-java\">RemoteFactory.requestRemote(RemoteTransactor.class, MainActivity.this, new Intent(&quot;atlas.transaction.intent.action.SECOND_TRANSACTION&quot;),\n        new RemoteFactory.OnRemoteStateListener&lt;RemoteTransactor&gt;() {\n            @Override\n            public void onRemotePrepared(RemoteTransactor iRemote) {\n                \/\/ if you want remote call you\n                iRemote.registerHostTransactor(new IRemote() {\n                    @Override\n                    public Bundle call(String s, Bundle bundle, IResponse iResponse) {\n                        Toast.makeText(RuntimeVariables.androidApplication, &quot;Command is &quot; + s, Toast.LENGTH_SHORT).show();\n                        return null;\n                    }\n\n                    @Override\n                    public &lt;T&gt; T getRemoteInterface(Class&lt;T&gt; aClass, Bundle bundle) {\n                        return null;\n                    }\n                });\n\n                ICaculator caculator = iRemote.getRemoteInterface(ICaculator.class, null);\n                Toast.makeText(RuntimeVariables.androidApplication, &quot;1+1 = &quot; + caculator.sum(1, 1), Toast.LENGTH_SHORT).show();\n\n                \/\/you can also use this\n\/\/                Bundle bundle = new Bundle();\n\/\/                bundle.putInt(&quot;num1&quot;, 1);\n\/\/                bundle.putInt(&quot;num2&quot;, 1);\n\/\/                Bundle result = iRemote.call(&quot;sum&quot;, bundle, null);\n\/\/                Toast.makeText(RuntimeVariables.androidApplication, &quot;1+1 = &quot; + result.getInt(&quot;result&quot;), Toast.LENGTH_SHORT).show();\n            }\n\n            @Override\n            public void onFailed(String s) {\n                Log.e(&quot;UserRemoteActivity&quot;, s);\n            }\n        });<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Atlas\u63d0\u4f9b\u4e863\u79cdBundle\u4e0eBundle\u4e4b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\uff1aRemoteTransactor\u3001RemoteV [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[],"class_list":["post-1227","post","type-post","status-publish","format-standard","hentry","category-atlas"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1227"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}