将OnActivityResult数据从电容器传递回混合应用程序



我正在开发一个Compacitor混合应用程序。我正在尝试按照本页中的说明进行操作:https://capacitor.ionicframework.com/docs/plugins/android,要启动ActivityForResult(来自混合javascript部分),请在本地secondActivity中执行一些操作,并将结果返回到混合部分。这里有一个类似的问题在其他地方被问到却没有答案!https://github.com/ionic-team/capacitor/issues/1044这是关于我的应用程序的简要描述:Ionic空白应用程序+电容器:这是我家里的代码。page.ts

import {Component} from '@angular/core';
import {Plugins} from '@capacitor/core';
//typeface
declare global {
interface PluginRegistry {
PluginTest2 ? : PluginTest2;
}
}
interface PluginTest2 {
goToActivity(): Promise < any > ;
}
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
goToSecondActivity() {
const {PluginTest2} = Plugins;
PluginTest2.goToActivity().then((result) => {
// I want data from SecondActivity back here and log it!
console.log(result);
})
}
}

home.page.html只是一个激活goToSecondActivity()的按钮;方法在将电容器项目打开到android工作室后,我添加了这个类:

package com.alihaghighicapacitor.qom;
import android.content.Intent;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
@NativePlugin()
public class PluginTest2 extends Plugin {
@PluginMethod()
public void goToActivity(PluginCall call) {
Intent intent = new Intent(getActivity().getApplicationContext(), secondActivity.class);
startActivityForResult(call, intent, 1);
// I can return this sillyData! back to "home.page.ts" but I want data from seconfActivity not this class
JSObject ret = new JSObject();
ret.put("added", "sillyData!");
call.success(ret);
}
@Override
protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
super.handleOnActivityResult(requestCode, resultCode, data);
String recievedMessage = data.getStringExtra("Data");
// capacitor team tutorial in the page I mentioned earlier instructs to use PluginCall as follows but IT DOES NOT WORK FOR ME!
PluginCall savedCall = getSavedCall();
if (savedCall == null) {
return;
}
if (requestCode == REQUEST_IMAGE_PICK) {
// Do something with the data
}
}
}

这是第二个Activity.java:

package com.alihaghighicapacitor.qom;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class secondActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
}
public void sendMeHome(View view) {
Intent intent = new Intent();
String message = editText.getText().toString();
intent.putExtra("Data", message);
finish();
}
}

和activity_second.xml 的相关部分

<Button android:id="@+id/button"
android:onClick="sendMeHome"/>
<EditText android:id="@+id/editText"/>
//clicking the button should get the editText value and send it back to
=> previous activity => to home.page.ts => and log it

此代码激发secondActivity,但除了我在上面的代码中评论的sillyData之外,在本机部分没有接收到任何数据!如果有人能帮忙,那对我来说将是一个很大的帮助。这是MAinActivity.java:

package com.alihaghighicapacitor.qom;
import com.alihaghighicapacitor.qom.PluginTest2;
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;
import java.util.ArrayList;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(PluginTest2.class);
}});
}
}

对于要调用的handleOnActivityResult(),您需要向@NativePlugin注册您的意向请求代码。

@NativePlugin(
requestCodes={MyPlugin.REQUEST_IMAGE_PICK}
)

意图与结果

在返回愚蠢的数据之前,必须使用saveCall(call);保存调用,否则savedCall将为null,并且它将转到空返回,而不是处理返回数据的代码。

相关内容

  • 没有找到相关文章

最新更新