|
代码要用代码块编辑发布,否则就会错乱的。
- /**
- * 启动游戏并传递授权code
- *
- * @param code 授权获取的code
- */
- private void launchGameWithCode(String code) {
- // 校验code合法性
- if (code == null || code.trim().isEmpty()) {
- Toast.makeText(this, "授权code为空,无法启动游戏", Toast.LENGTH_SHORT).show();
- return;
- }
- // 检查游戏是否安装
- if (!isAppInstalled("com.tencent.tmgp.sgame")) {
- Toast.makeText(this, "未检测到王者荣耀,请先安装", Toast.LENGTH_SHORT).show();
- return;
- }
- Intent intent = new Intent();
- intent.setPackage("com.tencent.tmgp.sgame"); // 王者荣耀包名
- intent.putExtra("code", code); // 传递授权code
- // 可添加额外参数标识来源
- intent.putExtra("source", getPackageName());
- try {
- startActivity(intent);
- } catch (ActivityNotFoundException e) {
- Toast.makeText(this, "启动失败:未找到游戏应用", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- } catch (SecurityException e) {
- Toast.makeText(this, "启动失败:权限不足", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- } catch (Exception e) {
- Toast.makeText(this, "启动失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- }
- /**
- * 检查应用是否安装
- *
- * @param packageName 应用包名
- * @return 是否安装
- */
- private boolean isAppInstalled(String packageName) {
- try {
- getPackageManager().getPackageInfo(packageName, 0);
- return true;
- } catch (PackageManager.NameNotFoundException e) {
- return false;
- }
- }
- @Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- // 更新当前Activity的Intent
- setIntent(intent);
- // 处理授权回调
- if (wxApi != null) {
- wxApi.handleIntent(intent, this);
- }
- }
复制代码
|
|