Android11 拉起微信、淘宝失败的基础性解决2024.1(附源码)

这两天想做一个软件管理平台,就想着拿打开微信和淘宝来练练手,但是跟着教程走完发现死活打不开,检查后发现是判断程序是否存在的判定过程出现问题,最后发现是安卓11收紧了相关权限,以上是经过进一步搜索和试验后给出的解决方案。

首先感谢各位大佬,相关链接如下:

#解决方案来自Android 11-第三方应用无法拉起微信适配 - 简书 (jianshu.com)icon-default.png?t=N7T8https://www.jianshu.com/p/993b3ad7be6d

 #进一步搜索发现安卓11收紧了权限,可以用过申请所有软件包的可见性来解决包名不确定的情况下的拉起,以下是相关博客,先在此记录,测试通过了更新博客:

Android 11以上如何通过包名拉起App - 掘金 (juejin.cn)icon-default.png?t=N7T8https://juejin.cn/post/7101284415300435982

 #常见包名:

Android常用的App包名整理 - 掘金 (juejin.cn)icon-default.png?t=N7T8https://juejin.cn/post/6865182194608898061

<更新:找到一步解决的方案:>

<!--申请申请所有软件包的可见性-->
    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent>
    </queries>

下面是我的代码:

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!--申请申请所有软件包的可见性-->
    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent>
    </queries>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Info_test"
        tools:targetApi="31">

        <activity
            android:name=".The_second_page"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 Mainactivity:

package com.example.info_test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        declares();

    }
    //把所有组件的声明放在一个方法内,简化程序主体
    public void declares(){
        Button taobao = (Button) findViewById(R.id.goto_TaoBao);
        Button wechat = (Button) findViewById(R.id.goto_WeChat);
        EditText taobao_order = (EditText) findViewById(R.id.taobao_order);
        EditText wechat_order = (EditText) findViewById(R.id.wechat_order);
        taobao.setOnClickListener(this);
        wechat.setOnClickListener(this);
        //第二种调用微信的组件
        Button wechat2 = (Button) findViewById(R.id.goto_WeChat2);
        wechat2.setOnClickListener(this);
        //拉起网易云音乐(尝试使用更高权限:申请所有包的可见性)
        Button Cloud_Music = (Button) findViewById(R.id.goto_CloudMusic);
        Cloud_Music.setOnClickListener(this);
    }
    //点击事件监听器
    //TODO 发现一个问题:不支持仅展示Toast

        @Override
        public void onClick(View view) {
            int viewId = view.getId();

            if (viewId == R.id.goto_TaoBao) {
                // 处理 taobao_order 的点击事件
                Toast.makeText(MainActivity.this, "接收到跳转指令,现在开始跳转", Toast.LENGTH_SHORT).show();
//                if (isAppInstalled(this,"com.taobao.taobao")) {
//                    Toast.makeText(MainActivity.this, "###成功检测到淘宝程序###", Toast.LENGTH_SHORT).show();
//
//                    Intent intent = new Intent();
//                    intent.setAction("Android.intent.action.VIEW");
//                    intent.setClassName("com.taobao.taobao", "com.taobao.tao.welcome.Welcome");
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    getApplication().startActivity(intent);
//                } else {
//                    Toast.makeText(MainActivity.this, "###检测到您并未安装淘宝###", Toast.LENGTH_SHORT).show();
//                }
                if (isAppInstalled(this, "com.taobao.taobao")) {
                    // 淘宝已安装,启动微信应用
                    startApp(this, "com.taobao.taobao");
                } else {
                    // 淘宝未安装,显示提示信息
                    Toast.makeText(this, "请先安装淘宝应用", Toast.LENGTH_SHORT).show();
                }
            }


            else if (viewId == R.id.goto_WeChat) {
                // 处理 goto_WeChat 的点击事件
                Toast.makeText(MainActivity.this, "接收到跳转指令,现在开始跳转", Toast.LENGTH_SHORT).show();
                if (isAppInstalled(this, "com.tencent.mm")) {
                    // 微信已安装,启动微信应用
                    startApp(this, "com.tencent.mm");
                } else {
                    // 微信未安装,显示提示信息
                    Toast.makeText(this, "请先安装微信应用", Toast.LENGTH_SHORT).show();
                }

            }
            else if (viewId == R.id.goto_WeChat2) {
                // 处理 goto_WeChat 的点击事件
                Toast.makeText(MainActivity.this, "接收到跳转指令,现在开始跳转", Toast.LENGTH_SHORT).show();
                Intent lan = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(lan.getComponent());
                startActivity(intent);

            }

            else if (viewId == R.id.goto_CloudMusic) {
                // 处理 goto_WeChat 的点击事件
                Toast.makeText(MainActivity.this, "接收到跳转指令,现在开始跳转", Toast.LENGTH_SHORT).show();
                if (isAppInstalled(this, "com.netease.cloudmusic")) {
                    // 微信已安装,启动微信应用
                    startApp(this, "com.netease.cloudmusic");
                } else {
                    // 微信未安装,显示提示信息
                    Toast.makeText(this, "***请先安装网易云音乐***", Toast.LENGTH_SHORT).show();
                }
            }

            else {
                // 处理其他情况
                Toast.makeText(MainActivity.this, "###出现未知错误,请排查 on choose ###", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, The_second_page.class);
                startActivity(intent);
            }
        }


    // 检查应用是否安装的方法
    private boolean isAppInstalled(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
            Log.d("AppInfo", "App Version: " + packageInfo.versionName);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("AppInfo", "App not found: " + packageName);
            return false;
        }
    }



    private void startApp(Context context, String packageName) {
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (launchIntent != null) {
            context.startActivity(launchIntent);
        } else {
            // 没有找到启动 Intent,可以尝试其他方式启动应用
            Toast.makeText(context, "无法启动应用", Toast.LENGTH_SHORT).show();
        }
    }



}

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="######程序调用测试######"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/taobao_order"
        android:hint="请输入想要搜索的商品:"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <Button
        android:id="@+id/goto_TaoBao"
        android:text="打开淘宝"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/wechat_order"
        android:hint="请输入您想要查找的联系人:"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>
    <Button
        android:id="@+id/goto_WeChat"
        android:text="跳转到微信"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />
    <Button
        android:id="@+id/goto_WeChat2"
        android:text="跳转到微信2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/goto_CloudMusic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="308dp"
        android:text="打开网易云音乐"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

</LinearLayout>

这样就能够解决安卓11检测程序存在的问题了,以上。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值