From 268688b8d8d293930f85e736705efbc80110e80e Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Fri, 12 Dec 2025 21:15:11 +0800 Subject: [PATCH] 20251212_211505_264 --- contacts/build.properties | 4 +- .../winboll/studio/contacts/MainActivity.java | 132 +++++++++++++++--- 2 files changed, 118 insertions(+), 18 deletions(-) diff --git a/contacts/build.properties b/contacts/build.properties index 93c74da..603b9ca 100644 --- a/contacts/build.properties +++ b/contacts/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Fri Dec 12 13:05:40 GMT 2025 +#Fri Dec 12 13:12:27 GMT 2025 stageCount=1 libraryProject= baseVersion=15.12 publishVersion=15.12.0 -buildCount=58 +buildCount=60 baseBetaVersion=15.12.1 diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/MainActivity.java b/contacts/src/main/java/cc/winboll/studio/contacts/MainActivity.java index d793a6f..87e07b5 100644 --- a/contacts/src/main/java/cc/winboll/studio/contacts/MainActivity.java +++ b/contacts/src/main/java/cc/winboll/studio/contacts/MainActivity.java @@ -4,14 +4,17 @@ import android.Manifest; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; +import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.ServiceConnection; import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.IBinder; import android.os.Looper; import android.telecom.TelecomManager; import android.telephony.PhoneStateListener; @@ -54,6 +57,7 @@ import java.util.List; * @Author ZhanGSKen&豆包大模型 * @Date 2025/08/30 14:32 * @Describe Contacts 主窗口(完全适配 API 30 + Java 7 语法) + * 核心优化:1. 与MainService状态联动 2. 移除高版本API依赖 3. 强化资源释放 4. Java7规范写法 */ public final class MainActivity extends WinBollActivity implements IWinBoLLActivity, ViewPager.OnPageChangeListener, View.OnClickListener { @@ -68,7 +72,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv private static final int REQUEST_OVERLAY_PERMISSION = 1003; private static final int REQUEST_CALL_SCREENING_PERMISSION = 1004; - // API版本硬编码常量(Java 7兼容) + // API版本硬编码常量(Java 7兼容,杜绝Build.VERSION_CODES高版本引用) private static final int ANDROID_6_API = 23; private static final int ANDROID_8_API = 26; private static final int ANDROID_10_API = 29; @@ -98,6 +102,9 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv private MyPhoneStateListener phoneStateListener; private List fragmentList; private List tabTitleList; + // 新增:ServiceConnection,用于与MainService绑定并传递窗口状态 + private MainServiceConnection mMainServiceConnection; + private boolean mIsServiceBound = false; // ====================== 6. 接口实现区 ====================== @Override @@ -117,6 +124,9 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv LogUtils.d(TAG, "===== onCreate: 主Activity开始创建 ====="); _MainActivity = this; + // 初始化ServiceConnection + mMainServiceConnection = new MainServiceConnection(); + // 直接初始化UI(原权限检查逻辑注释保留,按需启用) initUIAndLogic(savedInstanceState); LogUtils.d(TAG, "===== onCreate: 主Activity创建流程结束 ====="); @@ -135,6 +145,15 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv mADsBannerView.resumeADs(MainActivity.this); LogUtils.d(TAG, "onResume: 广告栏资源已恢复"); } + // 绑定MainService,用于传递窗口状态 + bindMainService(); + } + + @Override + protected void onPause() { + super.onPause(); + // 解绑MainService,避免内存泄漏 + unbindMainService(); } @Override @@ -151,6 +170,23 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); LogUtils.d(TAG, "onDestroy: 电话状态监听器已移除"); } + // 解绑MainService并通知窗口销毁状态 + unbindMainService(); + // 最后尝试绑定一次,传递销毁状态(防止解绑后未传递) + final Intent intent = new Intent(this, MainService.class); + bindService(intent, new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + MainService.MyBinder binder = (MainService.MyBinder) service; + binder.setMainWindowDestroyed(true); + unbindService(this); + LogUtils.d(TAG, "onDestroy: 已通知MainService窗口销毁状态"); + } + + @Override + public void onServiceDisconnected(ComponentName name) {} + }, Context.BIND_AUTO_CREATE); + LogUtils.d(TAG, "===== onDestroy: 主Activity销毁完成 ====="); } @@ -243,7 +279,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 权限拒绝提示对话框(Java 7 匿名内部类实现) + * 权限拒绝提示对话框(Java 7 匿名内部类实现,禁止Lambda) */ private void showPermissionDeniedDialogAndExit(String tip) { LogUtils.d(TAG, "showPermissionDeniedDialogAndExit: 弹出权限不足提示框"); @@ -308,7 +344,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv initPhoneStateListener(); LogUtils.d(TAG, "initUIAndLogic: 电话状态监听器初始化完成"); - // 6. 盾值视图初始化 + // 6. 盾值视图初始化(Java7分步写法,禁止链式调用) DunTemperatureView tempView = (DunTemperatureView) findViewById(R.id.dun_temp_view); tempView.setMaxValue(Rules.getInstance(this).getSettingsModel().getDunTotalCount()); tempView.setCurrentValue(Rules.getInstance(this).getSettingsModel().getDunCurrentCount()); @@ -325,7 +361,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 初始化ViewPager与Tab数据 + * 初始化ViewPager与Tab数据(Java7规范,泛型完整声明) */ private void initViewPagerAndTabs() { LogUtils.d(TAG, "initViewPagerAndTabs: 开始初始化ViewPager数据"); @@ -350,7 +386,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 初始化主服务 + * 初始化主服务(优化:延迟启动,降低主线程压力) */ private void initMainService() { LogUtils.d(TAG, "initMainService: 开始初始化主服务配置"); @@ -379,7 +415,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 初始化电话状态监听与通话筛选服务 + * 初始化电话状态监听与通话筛选服务(适配API30,移除高版本依赖) */ private void initPhoneStateListener() { LogUtils.d(TAG, "initPhoneStateListener: 开始初始化电话监听"); @@ -387,7 +423,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv phoneStateListener = new MyPhoneStateListener(); telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); - // API 10+ 启动通话筛选服务 + // API 10+ 启动通话筛选服务(硬编码版本判断) if (Build.VERSION.SDK_INT >= ANDROID_10_API) { Intent screeningIntent = new Intent(this, MyCallScreeningService.class); if (Build.VERSION.SDK_INT >= ANDROID_8_API) { @@ -401,7 +437,40 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv LogUtils.d(TAG, "initPhoneStateListener: 电话监听初始化完成"); } - // ====================== 10. 菜单相关函数区 ====================== + // ====================== 10. MainService绑定/解绑工具方法 ====================== + /** + * 绑定MainService,用于传递窗口状态 + */ + private void bindMainService() { + if (mIsServiceBound) { + return; + } + Intent intent = new Intent(this, MainService.class); + boolean bindSuccess = bindService(intent, mMainServiceConnection, Context.BIND_AUTO_CREATE); + if (bindSuccess) { + mIsServiceBound = true; + LogUtils.d(TAG, "bindMainService: 绑定MainService成功"); + } else { + LogUtils.e(TAG, "bindMainService: 绑定MainService失败"); + } + } + + /** + * 解绑MainService + */ + private void unbindMainService() { + if (mIsServiceBound && mMainServiceConnection != null) { + try { + unbindService(mMainServiceConnection); + mIsServiceBound = false; + LogUtils.d(TAG, "unbindMainService: 解绑MainService成功"); + } catch (IllegalArgumentException e) { + LogUtils.w(TAG, "unbindMainService: 服务未绑定,解绑失败", e); + } + } + } + + // ====================== 11. 菜单相关函数区 ====================== @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar_main, menu); @@ -419,7 +488,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv return super.onOptionsItemSelected(item); } - // ====================== 11. ViewPager页面回调区 ====================== + // ====================== 12. ViewPager页面回调区 ====================== @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @@ -435,11 +504,19 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv @Override public void onClick(View v) {} - // ====================== 12. 工具函数区 ====================== + // ====================== 13. 工具函数区 ====================== /** - * 拨号工具方法 + * 拨号工具方法(添加空指针防护) */ public static void dialPhoneNumber(String phoneNumber) { + if (_MainActivity == null) { + LogUtils.e(TAG, "dialPhoneNumber: MainActivity实例为空,无法拨号"); + return; + } + if (phoneNumber == null || phoneNumber.trim().isEmpty()) { + LogUtils.e(TAG, "dialPhoneNumber: 拨号号码为空"); + return; + } if (PermissionUtils.checkPermission(_MainActivity, Manifest.permission.CALL_PHONE)) { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + phoneNumber)); @@ -452,7 +529,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 判断是否为默认拨号应用 + * 判断是否为默认拨号应用(适配API30,硬编码版本判断) */ public boolean isDefaultPhoneCallApp() { if (Build.VERSION.SDK_INT >= ANDROID_6_API) { @@ -468,9 +545,13 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 检查服务是否正在运行 + * 检查服务是否正在运行(通用工具方法,添加空指针防护) */ public boolean isServiceRunning(Class serviceClass) { + if (serviceClass == null) { + LogUtils.e(TAG, "isServiceRunning: 服务类参数为null"); + return false; + } ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); if (manager == null) { LogUtils.w(TAG, "isServiceRunning: ActivityManager获取失败"); @@ -487,9 +568,28 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv return false; } - // ====================== 13. 内部类定义区(Java 7 规范) ====================== + // ====================== 14. 内部类定义区(Java 7 规范,禁止Lambda) ====================== /** - * ViewPager适配器(静态内部类减少内存泄漏风险) + * MainService连接类,用于传递窗口状态 + */ + private class MainServiceConnection implements ServiceConnection { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + LogUtils.d(TAG, "MainServiceConnection: 与MainService连接成功"); + MainService.MyBinder binder = (MainService.MyBinder) service; + // 通知MainService窗口处于活跃状态 + binder.setMainWindowDestroyed(false); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + LogUtils.w(TAG, "MainServiceConnection: 与MainService连接断开"); + mIsServiceBound = false; + } + } + + /** + * ViewPager适配器(静态内部类减少内存泄漏风险,Java7泛型完整声明) */ private static class MyPagerAdapter extends FragmentPagerAdapter { private final List fragmentList; @@ -519,7 +619,7 @@ public final class MainActivity extends WinBollActivity implements IWinBoLLActiv } /** - * 电话状态监听器 + * 电话状态监听器(Java7内部类规范写法) */ private class MyPhoneStateListener extends PhoneStateListener { @Override