Skip to content

Commit

Permalink
修复开启前台 Service 偶发崩溃的问题
Browse files Browse the repository at this point in the history
修复在部分三星手机上面无法触发点击事件的问题
修复日志过多的情况下进入 Logcat 页面会出现 ANR 的问题
  • Loading branch information
getActivity committed Nov 6, 2022
1 parent 722c5eb commit cdd3bed
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ assignees: getActivity

* 出现问题的安卓版本【必填】:请填写出现问题的 Android 版本

* 问题信息的来源渠道【必填】:请填写问题的来源(例如:自己遇到的/Bugly 看到的/用户反馈等等)

#### 请回答

* 是部分机型还是所有机型都会出现【必答】:部分/全部(例如:某为,某 Android 版本会出现)
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

![](logo.png)

* 项目地址:[Github](https://github.com/getActivity/Logcat)[码云](https://gitee.com/getActivity/Logcat)
* 项目地址:[Github](https://github.com/getActivity/Logcat)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/10.6/Logcat.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/10.8/Logcat.apk)

![](picture/demo_code.png)

Expand Down Expand Up @@ -51,11 +51,11 @@ dependencyResolutionManagement {
```groovy
dependencies {
// 日志调试框架:https://github.com/getActivity/Logcat
debugImplementation 'com.github.getActivity:Logcat:10.6'
debugImplementation 'com.github.getActivity:Logcat:10.8'
}
```

#### AndroidX
#### AndroidX 兼容

* 如果项目是基于 **AndroidX** 包,请在项目 `gradle.properties` 文件中加入

Expand Down Expand Up @@ -263,6 +263,8 @@ android.enableJetifier = true

* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)

* Android 资源大汇总:[AndroidIndex](https://github.com/getActivity/AndroidIndex) ![](https://img.shields.io/github/stars/getActivity/AndroidIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidIndex.svg)

* Android 开源排行榜:[AndroidGithubBoss](https://github.com/getActivity/AndroidGithubBoss) ![](https://img.shields.io/github/stars/getActivity/AndroidGithubBoss.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidGithubBoss.svg)

* Studio 精品插件:[StudioPlugins](https://github.com/getActivity/StudioPlugins) ![](https://img.shields.io/github/stars/getActivity/StudioPlugins.svg) ![](https://img.shields.io/github/forks/getActivity/StudioPlugins.svg)
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.logcat.demo"
minSdkVersion 16
targetSdkVersion 31
versionCode 1060
versionName "10.6"
versionCode 1080
versionName "10.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -62,11 +62,11 @@ dependencies {
implementation 'com.android.support:design:26.0.0'

// 标题栏:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:9.5'
implementation 'com.github.getActivity:TitleBar:9.6'

// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.github.getActivity:ToastUtils:10.5'

// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<!-- 悬浮窗入口 -->
<meta-data
android:name="LogcatWindowEntrance"
android:value="false" />
android:value="true" />

<!-- 通知栏入口 -->
<meta-data
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

defaultConfig {
minSdkVersion 16
versionCode 1060
versionName "10.6"
versionCode 1080
versionName "10.8"
}

lintOptions {
Expand All @@ -37,7 +37,7 @@ dependencies {
// noinspection GradleCompatible
implementation 'com.android.support:design:26.0.0'
// 悬浮窗框架:https://github.com/getActivity/XToast
implementation 'com.github.getActivity:XToast:8.5'
implementation 'com.github.getActivity:XToast:8.6'
}

tasks.withType(Javadoc) {
Expand Down
107 changes: 107 additions & 0 deletions library/src/main/java/com/hjq/logcat/ForegroundServiceStartTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.hjq.logcat;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/Logcat
* time : 2022/10/02
* desc : 开启前台 Service 任务
*/
final class ForegroundServiceStartTask implements Application.ActivityLifecycleCallbacks {

static void with(Application application) {
application.registerActivityLifecycleCallbacks(new ForegroundServiceStartTask());
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Intent intent = new Intent(activity, LogcatService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0 启动后台 service 出错 IllegalStateException: Not allowed to start service Intent
// 解决此问题的方案是将 startService 替换成 startForegroundService
// https://blog.csdn.net/u010784887/article/details/79675147
// Caused by: android.app.BackgroundServiceStartNotAllowedException:
// Not allowed to start service Intent { cmp=com.hjq.easy.demo/com.hjq.logcat.LogcatService }:
// app is in background uid UidRecord{48aba27 u0a399 CEM idle change:idle|cached procs:0 seq(0,0,0)}
// at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1889)
// at android.app.ContextImpl.startService(ContextImpl.java:1840)
// at android.content.ContextWrapper.startService(ContextWrapper.java:774)
// at com.hjq.logcat.LogcatProvider.onCreate(LogcatProvider.java:42)
// at android.content.ContentProvider.attachInfo(ContentProvider.java:2476)
// at android.content.ContentProvider.attachInfo(ContentProvider.java:2446)
// at android.app.ActivityThread.installProvider(ActivityThread.java:7915)
// at android.app.ActivityThread.installContentProviders(ActivityThread.java:7405)
// at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7159)
// at android.app.ActivityThread.access$1800(ActivityThread.java:284)
// at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2277)
// at android.os.Handler.dispatchMessage(Handler.java:106)
// at android.os.Looper.loopOnce(Looper.java:233)
// at android.os.Looper.loop(Looper.java:334)
// at android.app.ActivityThread.main(ActivityThread.java:8344)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)

// Android 12 启动后台 Service 出错;ForegroundServiceStartNotAllowedException:
// startForegroundService() not allowed due to mAllowStartForeground false: service xxx.LogcatService
// 解决此问题的方案是将前台 Service 启动时机挪动到第一个 Activity 启动的时候
// https://developer.android.google.cn/about/versions/12/foreground-services#cases-fgs-background-starts-allowed
// Caused by: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.hjq.easy.demo/com.hjq.logcat.LogcatService
// at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
// at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
// at android.os.Parcel.readParcelable(Parcel.java:3347)
// at android.os.Parcel.createExceptionOrNull(Parcel.java:2434)
// at android.os.Parcel.createException(Parcel.java:2423)
// at android.os.Parcel.readException(Parcel.java:2406)
// at android.os.Parcel.readException(Parcel.java:2348)
// at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6891)
// at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1875)
// at android.app.ContextImpl.startForegroundService(ContextImpl.java:1846)
// at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
// at com.hjq.logcat.LogcatProvider.onCreate(LogcatProvider.java:48)
// at android.content.ContentProvider.attachInfo(ContentProvider.java:2476)
// at android.content.ContentProvider.attachInfo(ContentProvider.java:2446)
// at android.app.ActivityThread.installProvider(ActivityThread.java:7915)
// at android.app.ActivityThread.installContentProviders(ActivityThread.java:7405)
// at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7159)
// at android.app.ActivityThread.access$1800(ActivityThread.java:284)
// at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2277)
// at android.os.Handler.dispatchMessage(Handler.java:106)
// at android.os.Looper.loopOnce(Looper.java:233)
// at android.os.Looper.loop(Looper.java:334)
// at android.app.ActivityThread.main(ActivityThread.java:8344)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
activity.startForegroundService(intent);
} else {
activity.startService(intent);
}

// 任务完成,移除 Activity 生命周期监听
activity.getApplication().unregisterActivityLifecycleCallbacks(this);
}

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityResumed(Activity activity) {}

@Override
public void onActivityPaused(Activity activity) {}

@Override
public void onActivityStopped(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {}
}
19 changes: 15 additions & 4 deletions library/src/main/java/com/hjq/logcat/LogcatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ protected void onCreate(Bundle savedInstanceState) {
mAdapter = new LogcatAdapter(this);
mAdapter.setOnItemClickListener(this);
mAdapter.setOnItemLongClickListener(this);
mRecyclerView.setAnimation(null);
mRecyclerView.setAdapter(mAdapter);

mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAnimation(null);
mCheckBox.setOnCheckedChangeListener(this);
mInputView.addTextChangedListener(this);

Expand All @@ -117,12 +117,22 @@ protected void onCreate(Bundle savedInstanceState) {
// 开始捕获
LogcatManager.start(this);

// mRecyclerView.setAdapter(mAdapter);
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
// 延迟设置适配器,这样可以提升性能,因为进入这个界面的时候,会频繁添加日志,如果这个时候适配器已经绑定
// 那么就会引发 itemView 频繁测试和绘制,这样会很卡,并且还会出现 ANR 的情况
mRecyclerView.setAdapter(mAdapter);
}
}, 1000);

mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
mLinearLayoutManager.scrollToPosition(mAdapter.getItemCount() - 1);
}
}, 500);
}, 1200);

initFilter();
refreshLayout();
Expand Down Expand Up @@ -246,7 +256,7 @@ public void onItemClick(LogcatInfo info, int position) {
}

@Override
public void onItemLongClick(LogcatInfo info, final int position) {
public boolean onItemLongClick(LogcatInfo info, final int position) {
new ChooseWindow(this)
.setList(R.string.logcat_options_copy, R.string.logcat_options_share, R.string.logcat_options_delete, R.string.logcat_options_shield)
.setListener(new ChooseWindow.OnListener() {
Expand All @@ -271,6 +281,7 @@ public void onSelected(final int location) {
}
})
.show();
return true;
}

private void copyLog(int position) {
Expand Down
Loading

0 comments on commit cdd3bed

Please sign in to comment.