Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fastdevelop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
高超
fastdevelop
Commits
fc1d4efe
Commit
fc1d4efe
authored
Sep 26, 2019
by
gao.chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
监听崩溃,保存本地
parent
f405ad01
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
181 additions
and
3 deletions
+181
-3
app/src/main/java/com/mayi/demo/MyApplication.java
+2
-0
fastDevelop/src/main/AndroidManifest.xml
+0
-3
fastDevelop/src/main/java/com/mayi/fastdevelop/comnon/CrashHandler.java
+179
-0
No files found.
app/src/main/java/com/mayi/demo/MyApplication.java
View file @
fc1d4efe
...
...
@@ -6,6 +6,7 @@ import com.gc.call.CallManage;
import
com.gc.call.CallParticipationBean
;
import
com.mayi.fastdevelop.base.BaseApplication
;
import
com.mayi.fastdevelop.comnon.Configure
;
import
com.mayi.fastdevelop.comnon.CrashHandler
;
import
com.mayi.fastdevelop.map.GoMapLocationCallTarget
;
import
com.mayi.fastdevelop.map.LocationCallTarget
;
import
com.mayi.fastdevelop.web.InitWebCallTarget
;
...
...
@@ -24,6 +25,7 @@ public class MyApplication extends BaseApplication {
CallManage
.
getInstance
().
handleTarget
(
bean
);
initBugly
(
"cb8018da1b"
,
true
);
Configure
.
APP_LOGO
=
R
.
mipmap
.
ic_launcher
;
CrashHandler
.
getInstance
().
init
(
this
);
}
@Override
...
...
fastDevelop/src/main/AndroidManifest.xml
View file @
fc1d4efe
...
...
@@ -17,9 +17,6 @@
<application>
<activity
android:name=
".commonpage.main.MainActivity"
/>
<activity
android:name=
".commonpage.login.LoginActivity"
/>
<!-- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException -->
<provider
android:name=
"android.support.v4.content.FileProvider"
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/comnon/CrashHandler.java
0 → 100644
View file @
fc1d4efe
package
com
.
mayi
.
fastdevelop
.
comnon
;
import
android.content.Context
;
import
android.content.pm.PackageInfo
;
import
android.content.pm.PackageManager
;
import
android.os.Build
;
import
android.os.Environment
;
import
android.os.Process
;
import
android.text.TextUtils
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.io.StringWriter
;
import
java.io.Writer
;
import
java.lang.reflect.Field
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 全局异常捕获类,当APP发生crash时,在存储卡包名目录下生成异常原因
*/
public
class
CrashHandler
implements
Thread
.
UncaughtExceptionHandler
{
private
static
CrashHandler
mInstance
;
private
Thread
.
UncaughtExceptionHandler
mDefaultHandler
;
private
Context
mContext
;
private
Map
<
String
,
String
>
mInfo
=
new
HashMap
<>();
private
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
private
CrashHandler
()
{
}
public
static
CrashHandler
getInstance
()
{
if
(
mInstance
==
null
)
{
synchronized
(
CrashHandler
.
class
)
{
if
(
mInstance
==
null
)
{
mInstance
=
new
CrashHandler
();
}
}
}
return
mInstance
;
}
public
void
init
(
Context
context
)
{
mDefaultHandler
=
Thread
.
getDefaultUncaughtExceptionHandler
();
Thread
.
setDefaultUncaughtExceptionHandler
(
this
);
mContext
=
context
;
}
@Override
public
void
uncaughtException
(
Thread
t
,
Throwable
e
)
{
if
(!
handleException
(
e
))
{
//未处理,调用系统默认的处理器处理,就是烦人的CRASH弹出框
if
(
null
!=
mDefaultHandler
)
{
mDefaultHandler
.
uncaughtException
(
t
,
e
);
}
}
else
{
//已经人为处理
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e1
)
{
e1
.
printStackTrace
();
}
Process
.
killProcess
(
Process
.
myPid
());
System
.
exit
(
1
);
}
}
private
boolean
handleException
(
Throwable
e
)
{
if
(
e
==
null
)
{
return
false
;
}
collectErrorInfo
();
saveErrorInfo
(
e
);
return
true
;
}
private
void
collectErrorInfo
()
{
PackageManager
pm
=
mContext
.
getPackageManager
();
try
{
//取APP版本信息
PackageInfo
pi
=
pm
.
getPackageInfo
(
mContext
.
getPackageName
(),
PackageManager
.
GET_ACTIVITIES
);
if
(
null
!=
pi
)
{
String
versionName
=
TextUtils
.
isEmpty
(
pi
.
packageName
)
?
"未设置版本名称"
:
pi
.
versionName
;
String
versionCode
=
pi
.
versionCode
+
""
;
mInfo
.
put
(
"versionName"
,
versionName
);
mInfo
.
put
(
"versionCode"
,
versionCode
);
}
//取硬件环境信息
Field
[]
fields
=
Build
.
class
.
getFields
();
if
(
null
!=
fields
)
{
for
(
Field
field
:
fields
)
{
field
.
setAccessible
(
true
);
try
{
mInfo
.
put
(
field
.
getName
(),
field
.
get
(
null
).
toString
());
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
}
}
//取APP运行时内存信息
Float
totalMemory
=
Runtime
.
getRuntime
().
totalMemory
()
*
1.0f
/
(
1024
*
1024
);
Float
freeMemory
=
Runtime
.
getRuntime
().
freeMemory
()
*
1.0f
/
(
1024
*
1024
);
Float
maxMemory
=
Runtime
.
getRuntime
().
maxMemory
()
*
1.0f
/
(
1024
*
1024
);
mInfo
.
put
(
"totalMemory"
,
totalMemory
+
""
);
mInfo
.
put
(
"freeMemory"
,
freeMemory
+
""
);
mInfo
.
put
(
"maxMemory"
,
maxMemory
+
""
);
}
catch
(
PackageManager
.
NameNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
private
void
saveErrorInfo
(
Throwable
e
)
{
StringBuffer
stringBuffer
=
new
StringBuffer
();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
mInfo
.
entrySet
())
{
String
keyName
=
entry
.
getKey
();
String
value
=
entry
.
getValue
();
stringBuffer
.
append
(
keyName
+
"="
+
value
+
"\n"
);
}
Writer
writer
=
new
StringWriter
();
PrintWriter
printWriter
=
new
PrintWriter
(
writer
);
e
.
printStackTrace
(
printWriter
);
Throwable
cause
=
e
.
getCause
();
//一直写直到写完
while
(
cause
!=
null
)
{
cause
.
printStackTrace
(
printWriter
);
cause
=
cause
.
getCause
();
}
String
result
=
writer
.
toString
();
stringBuffer
.
append
(
result
);
long
curTime
=
System
.
currentTimeMillis
();
String
time
=
dateFormat
.
format
(
new
Date
());
String
fileName
=
"crash-"
+
time
+
"-"
+
curTime
+
".txt"
;
if
(
Environment
.
getExternalStorageState
().
equals
(
Environment
.
MEDIA_MOUNTED
))
{
//存在SD卡
String
path
=
Environment
.
getExternalStorageDirectory
().
getPath
()
+
File
.
separator
+
mContext
.
getPackageName
()
+
File
.
separator
;
File
dir
=
new
File
(
path
);
if
(!
dir
.
exists
())
{
dir
.
mkdirs
();
}
FileOutputStream
fos
=
null
;
try
{
fos
=
new
FileOutputStream
(
path
+
fileName
);
fos
.
write
(
stringBuffer
.
toString
().
getBytes
());
}
catch
(
FileNotFoundException
e1
)
{
e1
.
printStackTrace
();
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
finally
{
try
{
fos
.
close
();
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment