我正在开发一个Compacitor混合应用程序。我试图按照此页面中的说明操作:https://capacitor.ionicframework.com/docs/plugins/android,以启动ActivityForResult(从混合JavaScript部分),在本地secondActivity中做一些事情并将结果返回到混合部分。这里是其他地方问的类似问题没有答案!https://github.com/ionic-team/capacitor/issues/1044这是关于我的应用程序的简要描述:一个离子空白应用程序+电容器:这是我家的代码.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 studio中打开capacitor项目后,我添加了这个类:
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
}
}
}
型
这里是secondActivity.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!如果anyBody可以帮助,它将对我有很大的帮助。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);
}});
}
}
型
2条答案
按热度按时间lzfw57am1#
要调用
handleOnActivityResult()
,您需要向@NativePlugin
注册Intent请求代码。字符串
意图和结果
92vpleto2#
在返回愚蠢的数据之前,你必须用
saveCall(call);
保存调用,否则savedCall将为null,它将转到空返回,而不是处理返回数据的代码。