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,如有侵權,請聯繫刪除。