Flutter的安卓项目跳转谷歌市场
没找到相关资料 使用的跟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")){
String packageName=methodCall.argument("packageName");
launchAppDetail(packageName,"com.android.vending");
result.success(true);
}else {
result.notImplemented();
}
}
/**
* 启动到应用商店app详情界面
*
* @param appPkg 目标App的包名
* @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面,某些应用商店可能会失败
*/
@TargetApi(Build.VERSION_CODES.DONUT)
public void launchAppDetail(String appPkg, String marketPkg) {
try {
if (TextUtils.isEmpty(appPkg)) return;
Uri uri = Uri.parse("market://details?id=" + appPkg);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (!TextUtils.isEmpty(marketPkg)) {
intent.setPackage(marketPkg);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:如果 应用商店包名为空 就会将手机上已下载的应用商店都列出来,让你选择一个进行跳转。
2.在MainActivity中注册
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
registerCustomPlugin(this)
}
fun registerCustomPlugin(registry: PluginRegistry){
GooglePlayPlugin.registerWith(registry.registrarFor(GooglePlayPlugin.CHANNEL))
}
}
3.在flutter中调用方法
///跳转谷歌市场
void goGooglePlayMarket() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
//包名
String packageName = packageInfo.packageName;
print('===$packageName');
const platform = MethodChannel('googleplay');
try {
await platform
.invokeMethod('entergoogle', {'packageName': packageName});
} on PlatformException catch (e) {
print(e);
}
}
主流应用商店对应的包名如下:
包名 | 商店 |
---|---|
com.android.vending | Google Play |
com.tencent.android.qqdownloader | 应用宝 |
com.qihoo.appstore | 360手机助手 |
com.baidu.appsearch | 百度手机助 |
com.xiaomi.market | 小米应用商店 |
com.wandoujia.phoenix2 | 豌豆荚 |
com.huawei.appmarket | 华为应用市场 |
com.taobao.appcenter | 淘宝手机助手 |
com.hiapk.marketpho | 安卓市场 |
cn.goapk.market | 安智市场 |
注意:
第1歩中调用launchAppDetail(String appPkg, String marketPkg)方法的marketPkg根据你想跳转的商店来选择,如果是谷歌市场就使用 com.android.vending,其他市场就在上面表格寻找对应的包名。
本文部分参考https://www.jianshu.com/p/a4a806567368,如有侵权,请联系删除。