Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
android_youyou
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
android
android_youyou
Commits
59811504
Commit
59811504
authored
May 13, 2021
by
jyx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加友盟工具类
parent
b1abd5e6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
136 additions
and
0 deletions
+136
-0
UmengUtil.java
...c/main/java/com/wenshu/youyou/ui/activitys/UmengUtil.java
+136
-0
No files found.
app/src/main/java/com/wenshu/youyou/ui/activitys/UmengUtil.java
0 → 100644
View file @
59811504
package
com
.
wenshu
.
youyou
.
ui
.
activitys
;
import
android.Manifest
;
import
android.annotation.TargetApi
;
import
android.content.Context
;
import
android.content.pm.PackageManager
;
import
android.net.wifi.WifiInfo
;
import
android.net.wifi.WifiManager
;
import
android.os.Build
;
import
android.text.TextUtils
;
import
java.lang.reflect.Method
;
import
java.net.NetworkInterface
;
import
java.util.Enumeration
;
import
java.util.Locale
;
/**
* @author jyx
* @date 2021/4/30
* @des
*/
public
class
UmengUtil
{
public
static
String
getDeviceInfo
(
Context
context
)
{
try
{
org
.
json
.
JSONObject
json
=
new
org
.
json
.
JSONObject
();
android
.
telephony
.
TelephonyManager
tm
=
(
android
.
telephony
.
TelephonyManager
)
context
.
getSystemService
(
Context
.
TELEPHONY_SERVICE
);
String
device_id
=
null
;
if
(
checkPermission
(
context
,
Manifest
.
permission
.
READ_PHONE_STATE
))
{
device_id
=
tm
.
getDeviceId
();
}
String
mac
=
getMac
(
context
);
json
.
put
(
"mac"
,
mac
);
if
(
TextUtils
.
isEmpty
(
device_id
))
{
device_id
=
mac
;
}
if
(
TextUtils
.
isEmpty
(
device_id
))
{
device_id
=
android
.
provider
.
Settings
.
Secure
.
getString
(
context
.
getContentResolver
(),
android
.
provider
.
Settings
.
Secure
.
ANDROID_ID
);
}
json
.
put
(
"device_id"
,
device_id
);
return
json
.
toString
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
String
getMac
(
Context
context
)
{
String
mac
=
""
;
if
(
context
==
null
)
{
return
mac
;
}
if
(
Build
.
VERSION
.
SDK_INT
<
23
)
{
mac
=
getMacBySystemInterface
(
context
);
}
else
{
mac
=
getMacByJavaAPI
();
if
(
TextUtils
.
isEmpty
(
mac
))
{
mac
=
getMacBySystemInterface
(
context
);
}
}
return
mac
;
}
@TargetApi
(
9
)
private
static
String
getMacByJavaAPI
()
{
try
{
Enumeration
<
NetworkInterface
>
interfaces
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
interfaces
.
hasMoreElements
())
{
NetworkInterface
netInterface
=
interfaces
.
nextElement
();
if
(
"wlan0"
.
equals
(
netInterface
.
getName
())
||
"eth0"
.
equals
(
netInterface
.
getName
()))
{
byte
[]
addr
=
netInterface
.
getHardwareAddress
();
if
(
addr
==
null
||
addr
.
length
==
0
)
{
return
null
;
}
StringBuilder
buf
=
new
StringBuilder
();
for
(
byte
b
:
addr
)
{
buf
.
append
(
String
.
format
(
"%02X:"
,
b
));
}
if
(
buf
.
length
()
>
0
)
{
buf
.
deleteCharAt
(
buf
.
length
()
-
1
);
}
return
buf
.
toString
().
toLowerCase
(
Locale
.
getDefault
());
}
}
}
catch
(
Throwable
e
)
{
}
return
null
;
}
private
static
String
getMacBySystemInterface
(
Context
context
)
{
if
(
context
==
null
)
{
return
""
;
}
try
{
WifiManager
wifi
=
(
WifiManager
)
context
.
getSystemService
(
Context
.
WIFI_SERVICE
);
if
(
checkPermission
(
context
,
Manifest
.
permission
.
ACCESS_WIFI_STATE
))
{
WifiInfo
info
=
wifi
.
getConnectionInfo
();
return
info
.
getMacAddress
();
}
else
{
return
""
;
}
}
catch
(
Throwable
e
)
{
return
""
;
}
}
public
static
boolean
checkPermission
(
Context
context
,
String
permission
)
{
boolean
result
=
false
;
if
(
context
==
null
)
{
return
result
;
}
if
(
Build
.
VERSION
.
SDK_INT
>=
23
)
{
try
{
Class
clazz
=
Class
.
forName
(
"android.content.Context"
);
Method
method
=
clazz
.
getMethod
(
"checkSelfPermission"
,
String
.
class
);
int
rest
=
(
Integer
)
method
.
invoke
(
context
,
permission
);
if
(
rest
==
PackageManager
.
PERMISSION_GRANTED
)
{
result
=
true
;
}
else
{
result
=
false
;
}
}
catch
(
Throwable
e
)
{
result
=
false
;
}
}
else
{
PackageManager
pm
=
context
.
getPackageManager
();
if
(
pm
.
checkPermission
(
permission
,
context
.
getPackageName
())
==
PackageManager
.
PERMISSION_GRANTED
)
{
result
=
true
;
}
}
return
result
;
}
}
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