我正在开发Compacitor混合应用程序。我正在尝试遵循此页面中的说明:https://capacitor.ionicframework.com/docs/plugins/android,要启动startActivityForResult(来自混合javascript部分),请在本机secondActivity中执行一些操作,然后将结果返回到混合部分。 这是类似的问题在其他地方问无答案! https://github.com/ionic-team/capacitor/issues/1044 这是关于我的应用程序的简短说明: 离子空白应用程序+电容器: 这是我home.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中之后,我添加了此类:
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之外,本机部分未接收到任何数据! 如果有什么可以帮助的,那对我来说将是一个很大的帮助。 这是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);
}});
}
}
答案 0 :(得分:1)
要调用handleOnActivityResult()
,您需要向@NativePlugin
注册您的意图请求代码。
@NativePlugin(
requestCodes={MyPlugin.REQUEST_IMAGE_PICK}
)
答案 1 :(得分:0)
在返回愚蠢的数据之前,必须使用saveCall(call);
保存该调用,否则saveCall将为null,它将转到空返回值,而不是处理返回数据的代码。