增加微信是否安装判断

This commit is contained in:
2025-07-22 09:11:58 +08:00
parent b3fcc019f8
commit 1d2fc9f172
5 changed files with 49 additions and 19 deletions

View File

@@ -25,6 +25,7 @@ import com.hjq.toast.Toaster;
import com.tencent.mmkv.MMKV;
import com.ttstd.elderlyassistant.bean.ContactInfo;
import com.ttstd.elderlyassistant.config.CommonConfig;
import com.ttstd.elderlyassistant.utils.ApkUtils;
import java.util.List;
import java.util.Optional;
@@ -119,8 +120,8 @@ public class SelectToSpeakService extends AccessibilityService {
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: ");
if (intent != null) {
mContact = (ContactInfo) intent.getSerializableExtra("WechatInfo");
Log.e(TAG, "onStartCommand: wechatInfo = " + mContact);
mContact = (ContactInfo) intent.getSerializableExtra("ContactInfo");
Log.e(TAG, "onStartCommand: contactInfo = " + mContact);
if (mContact != null) {
mName = mContact.getName();
mCallType = mContact.getOperation();
@@ -129,7 +130,6 @@ public class SelectToSpeakService extends AccessibilityService {
} else {
Toaster.showLong("没有获取到联系人数据");
}
}
return super.onStartCommand(intent, flags, startId);
}
@@ -301,18 +301,23 @@ public class SelectToSpeakService extends AccessibilityService {
* 打开微信
*/
private void launchWeChat() {
Intent intent = new Intent();
ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setComponent(cmp);
try {
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, "launchWeChat: " + e.getMessage());
if (ApkUtils.isInstalled(SelectToSpeakService.this, "com.tencent.mm")) {
Intent intent = new Intent();
ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setComponent(cmp);
try {
startActivity(intent);
Toaster.show("打开微信失败,请检查微信版本是否过低");
} catch (Exception e) {
Log.e(TAG, "launchWeChat: " + e.getMessage());
}
} else {
Toaster.show("微信未安装,请安装微信后使用");
}
}

View File

@@ -135,7 +135,7 @@ public class MainActivity extends AppCompatActivity {
try {
ContactInfo contact = gson.fromJson(textRecord, type);
Intent accessibilityIntent = new Intent(MainActivity.this, SelectToSpeakService.class);
accessibilityIntent.putExtra("WechatInfo", contact);
accessibilityIntent.putExtra("ContactInfo", contact);
startService(accessibilityIntent);
} catch (Exception e) {
Log.e(TAG, "resolveIntent: " + e.getMessage());

View File

@@ -139,7 +139,7 @@ public class SchemeActivity extends AppCompatActivity {
case 3:
case 4:
Intent accessibilityIntent = new Intent(SchemeActivity.this, SelectToSpeakService.class);
accessibilityIntent.putExtra("WechatInfo", contactInfo);
accessibilityIntent.putExtra("ContactInfo", contactInfo);
startService(accessibilityIntent);
break;
case 5:

View File

@@ -0,0 +1,23 @@
package com.ttstd.elderlyassistant.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
public class ApkUtils {
public static boolean isInstalled(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) return false;
PackageManager packageManager = context.getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return packageInfo != null;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return false;
}
}

View File

@@ -196,9 +196,10 @@
android:layout_width="match_parent"
android:layout_height="40dp"
android:checked="@{contactInfo.operation==5}"
android:enabled="false"
android:maxLines="1"
android:singleLine="true"
android:text="拨打QQ电话" />
android:text="拨打QQ电话(即将开放)" />
<RadioButton
@@ -206,9 +207,10 @@
android:layout_width="match_parent"
android:layout_height="40dp"
android:checked="@{contactInfo.operation==6}"
android:enabled="false"
android:maxLines="1"
android:singleLine="true"
android:text="拨打QQ视频" />
android:text="拨打QQ视频(即将开放)" />
</RadioGroup>