在Flutter的Android项目中实现跳转至Google Play商店。(flutter.io)(flutter.io)

在安卓平台上,Flutter项目可以实现跳转至谷歌市场。

很遗憾,我无法提供与Android交互的相关资料。不过,我会先记录下使用的方法。

在Flutter的Android项目中实现跳转至Google Play商店。(flutter.io)-第1张图片-谷歌商店上架

在Android项目的目录结构中,通过Project视图,在android-app-src-main-kotlin-包名目录下进行操作。

1.新建GooglePlayPlugin文件

package com.demo.xxx;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
/**
* 跳转应用市场
*/
public class GooglePlayPlugin implements MethodChannel.MethodCallHandler {
static MethodChannel channel;
private Activity activity;
public static String CHANNEL = googleplay; // 分析1
GooglePlayPlugin(Activity activity) {
this.activity = activity;
}
public static void registerWith(PluginRegistry.Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), CHANNEL);
GooglePlayPlugin instance = new GooglePlayPlugin(registrar.activity());
channel.setMethodCallHandler(instance);
}
@SuppressLint(WrongConstant)
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals(entergoogle)) {
}
}
} 

2. 在主活动(MainActivity)中进行注册

```kotlin
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
registerCustomPlugin(this)
}
private fun registerCustomPlugin(registry: PluginRegistry) {
GooglePlayPlugin.registerWith(registry.registrarFor(GooglePlayPlugin.CHANNEL))
}
}
```
在Flutter中调用该方法的步骤如下:
1. 在需要调用的地方导入`platform_channels.dart`包。
2. 创建一个`MethodChannel`对象,指定与原生代码通信的通道名称。
3. 调用该方法,例如:`await platform.invokeMethod('registerCustomPlugin');`
4. 如果需要传递参数给原生代码,可以在第三步中添加参数。 
///跳转至谷歌市场
void goGooglePlayMarket() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
//获取包名
String packageName = packageInfo.packageName;
print('包名为:$packageName');
const platform = ... 包名商店com.android.vendingGoogle Playcom.tencent.android.qqdownloader应用宝com.qihoo.appstore360手机助手com.baidu.appsearch百度手机助com.xiaomi.market小米应用商店com.wandoujia.phoenix2豌豆荚com.huawei.appmarket华为应用市场com.taobao.appcenter淘宝手机助手com.hiapk.marketpho安卓市场cn.goapk.market安智市场   

注意:

在调用launchAppDetail(String appPkg, String marketPkg)方法时,你可以根据你想要跳转的商店来选择marketPkg。如果你想要跳转到谷歌市场,就使用com.android.vending作为marketPkg;如果是其他市场,可以在上面的表格中找到对应的包名。

本文部分内容参考自https://www.jianshu.com/p/a4a806567368,如有侵权,请联系我们删除。

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注