增加微信是否安装判断

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.tencent.mmkv.MMKV;
import com.ttstd.elderlyassistant.bean.ContactInfo; import com.ttstd.elderlyassistant.bean.ContactInfo;
import com.ttstd.elderlyassistant.config.CommonConfig; import com.ttstd.elderlyassistant.config.CommonConfig;
import com.ttstd.elderlyassistant.utils.ApkUtils;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -119,8 +120,8 @@ public class SelectToSpeakService extends AccessibilityService {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: "); Log.e(TAG, "onStartCommand: ");
if (intent != null) { if (intent != null) {
mContact = (ContactInfo) intent.getSerializableExtra("WechatInfo"); mContact = (ContactInfo) intent.getSerializableExtra("ContactInfo");
Log.e(TAG, "onStartCommand: wechatInfo = " + mContact); Log.e(TAG, "onStartCommand: contactInfo = " + mContact);
if (mContact != null) { if (mContact != null) {
mName = mContact.getName(); mName = mContact.getName();
mCallType = mContact.getOperation(); mCallType = mContact.getOperation();
@@ -129,7 +130,6 @@ public class SelectToSpeakService extends AccessibilityService {
} else { } else {
Toaster.showLong("没有获取到联系人数据"); Toaster.showLong("没有获取到联系人数据");
} }
} }
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
} }
@@ -301,18 +301,23 @@ public class SelectToSpeakService extends AccessibilityService {
* 打开微信 * 打开微信
*/ */
private void launchWeChat() { private void launchWeChat() {
Intent intent = new Intent(); if (ApkUtils.isInstalled(SelectToSpeakService.this, "com.tencent.mm")) {
ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"); Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN); ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setAction(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setComponent(cmp); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
try { intent.setComponent(cmp);
startActivity(intent); try {
} catch (Exception e) { startActivity(intent);
Log.e(TAG, "launchWeChat: " + e.getMessage()); 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 { try {
ContactInfo contact = gson.fromJson(textRecord, type); ContactInfo contact = gson.fromJson(textRecord, type);
Intent accessibilityIntent = new Intent(MainActivity.this, SelectToSpeakService.class); Intent accessibilityIntent = new Intent(MainActivity.this, SelectToSpeakService.class);
accessibilityIntent.putExtra("WechatInfo", contact); accessibilityIntent.putExtra("ContactInfo", contact);
startService(accessibilityIntent); startService(accessibilityIntent);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "resolveIntent: " + e.getMessage()); Log.e(TAG, "resolveIntent: " + e.getMessage());

View File

@@ -139,7 +139,7 @@ public class SchemeActivity extends AppCompatActivity {
case 3: case 3:
case 4: case 4:
Intent accessibilityIntent = new Intent(SchemeActivity.this, SelectToSpeakService.class); Intent accessibilityIntent = new Intent(SchemeActivity.this, SelectToSpeakService.class);
accessibilityIntent.putExtra("WechatInfo", contactInfo); accessibilityIntent.putExtra("ContactInfo", contactInfo);
startService(accessibilityIntent); startService(accessibilityIntent);
break; break;
case 5: 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_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:checked="@{contactInfo.operation==5}" android:checked="@{contactInfo.operation==5}"
android:enabled="false"
android:maxLines="1" android:maxLines="1"
android:singleLine="true" android:singleLine="true"
android:text="拨打QQ电话" /> android:text="拨打QQ电话(即将开放)" />
<RadioButton <RadioButton
@@ -206,9 +207,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:checked="@{contactInfo.operation==6}" android:checked="@{contactInfo.operation==6}"
android:enabled="false"
android:maxLines="1" android:maxLines="1"
android:singleLine="true" android:singleLine="true"
android:text="拨打QQ视频" /> android:text="拨打QQ视频(即将开放)" />
</RadioGroup> </RadioGroup>