我尝试从我的 Flutter/Android 项目中调用 Intent 请求。
如上所述,应按如下方式完成
Intent intent = new Intent();
intent.setComponent(new ComponentName("sk.co.xxx.yyy", "sk.co.xxx.yyy.MainActivity"));
JSONObject jReq = new JSONObject();
String sReq="";
jReq.put("Amount",<Amount>);
jReq.put("Operation",<Operation>);
jReq.put("TransactionID",<can be generated e.g.getRandom()>);
sReq = jReq.toString();
if(sReq.isEmpty())return;
intent.putExtra("POS_EMULATOR_EXTRA", sReq); try {
startActivityForResult(intent, <requestCode>); }
Flutter下如何实现这段代码?我尝试使用来自 flutter.dev 的 android_intent 包,但我收到以下错误消息:
E/MethodChannel#plugins.flutter.io/android_intent(22815): Failed to handle method call
E/MethodChannel#plugins.flutter.io/android_intent(22815): android.content.ActivityNotFoundException: No Activity found to handle Intent
感谢您的任何回答!我的 Flutter 代码,用来替换上面的代码,是:
if (Platform.isAndroid) {
Map data = {
"Amount": "$amount",
"Operation": "$operation"
};
AndroidIntent intent = AndroidIntent(
componentName: "sk.co.xxx.yyy",
data: data.toString()
);
await intent.launch();
我明白了,“MainActivity”和 putExtra(“POS_EMULATOR_EXTRA”不在我的 Flutter 代码中,但我现在没有,我该如何实现......
最佳答案
实际答案可以在 AndroidManifest.xml
中找到。 , 因为 ActivityNotFoundException
可以按字面意思理解……这意味着无法解析 Java 包或类名。
String packageName = getApplicationContext().getPackageName(); // just for example
String className = packageName + ".SomeActivity"; // just for example
Intent intent = new Intent(Intent.ACTION_MAIN); // just for example
intent.setComponent(new ComponentName(packageName, className));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, e.getMessage());
// in here one still could perform a fall-back operation
}
Activity
也解决必须有android:exported="true"
宣布。用
try
没有错/catch
在那里,尤其是当它不是自己的包时。关键部分是
intent-filter
元素;当这不匹配时,无论 Activity
存在并被导出——这一切都基于 Intent 过滤器的工作方式。
关于android - flutter : Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65780266/