Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
D
Daemon_App
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
胡江林
Daemon_App
Commits
8099d45f
Commit
8099d45f
authored
Aug 10, 2022
by
胡江林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
针对Android8.0启动服务处理,getRunningService无效处理。
parent
700e4f16
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
66 deletions
+66
-66
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+4
-0
app/src/main/java/com/youbay/daemon/BootReceiver.java
app/src/main/java/com/youbay/daemon/BootReceiver.java
+12
-1
app/src/main/java/com/youbay/daemon/DaemonService.java
app/src/main/java/com/youbay/daemon/DaemonService.java
+40
-58
app/src/main/java/com/youbay/daemon/MainActivity.java
app/src/main/java/com/youbay/daemon/MainActivity.java
+10
-7
No files found.
app/src/main/AndroidManifest.xml
View file @
8099d45f
...
@@ -4,7 +4,11 @@
...
@@ -4,7 +4,11 @@
<uses-permission
android:name=
"app.custom.permission"
/>
<uses-permission
android:name=
"app.custom.permission"
/>
<uses-permission
android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
<uses-permission
android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
<uses-permission
android:name=
"android.permission.FOREGROUND_SERVICE"
/>
<queries>
<package
android:name=
"com.youbay.classcard"
/>
</queries>
<application
<application
android:allowBackup=
"true"
android:allowBackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:icon=
"@mipmap/ic_launcher"
...
...
app/src/main/java/com/youbay/daemon/BootReceiver.java
View file @
8099d45f
...
@@ -6,6 +6,7 @@ import android.content.BroadcastReceiver;
...
@@ -6,6 +6,7 @@ import android.content.BroadcastReceiver;
import
android.content.ComponentName
;
import
android.content.ComponentName
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.os.Build
;
/**
/**
* @author Administrator
* @author Administrator
...
@@ -34,13 +35,23 @@ public class BootReceiver extends BroadcastReceiver {
...
@@ -34,13 +35,23 @@ public class BootReceiver extends BroadcastReceiver {
}
}
private
void
startIntent
(
Context
context
)
{
private
void
startIntent
(
Context
context
)
{
if
(
isServiceRunning
(
context
))
{
/*if (isServiceRunning(context)) {
System.out.println("this daemon service is running ..");
System.out.println("this daemon service is running ..");
} else {
} else {
final Intent daemonIntent = new Intent();
final Intent daemonIntent = new Intent();
daemonIntent.setAction(DAEMON_SERVICE_ACTION);
daemonIntent.setAction(DAEMON_SERVICE_ACTION);
daemonIntent.setComponent(new ComponentName(APP_PACKAGE_NAME, DAEMON_SERVICE_NAME));
daemonIntent.setComponent(new ComponentName(APP_PACKAGE_NAME, DAEMON_SERVICE_NAME));
context.startService(daemonIntent);
context.startService(daemonIntent);
}*/
final
Intent
daemonIntent
=
new
Intent
();
daemonIntent
.
setAction
(
DAEMON_SERVICE_ACTION
);
daemonIntent
.
setComponent
(
new
ComponentName
(
APP_PACKAGE_NAME
,
DAEMON_SERVICE_NAME
));
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
context
.
startForegroundService
(
daemonIntent
);
}
else
{
context
.
startService
(
daemonIntent
);
}
}
}
}
...
...
app/src/main/java/com/youbay/daemon/DaemonService.java
View file @
8099d45f
package
com
.
youbay
.
daemon
;
package
com
.
youbay
.
daemon
;
import
android.app.ActivityManager
;
import
android.app.Notification
;
import
android.app.NotificationChannel
;
import
android.app.NotificationManager
;
import
android.app.Service
;
import
android.app.Service
;
import
android.content.ComponentName
;
import
android.content.ComponentName
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.os.Bu
ndle
;
import
android.os.Bu
ild
;
import
android.os.IBinder
;
import
android.os.IBinder
;
import
android.os.Message
;
import
android.util.Log
;
import
android.util.Log
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -26,28 +25,40 @@ public class DaemonService extends Service {
...
@@ -26,28 +25,40 @@ public class DaemonService extends Service {
private
static
final
String
APP_SERVICE_NAME
=
"com.youbay.classcard.service.DaemonService"
;
private
static
final
String
APP_SERVICE_NAME
=
"com.youbay.classcard.service.DaemonService"
;
private
static
final
String
APP_SERVICE_ACTION
=
"com.youbay.classcard.service.DaemonService"
;
private
static
final
String
APP_SERVICE_ACTION
=
"com.youbay.classcard.service.DaemonService"
;
private
boolean
isRunning
=
false
;
private
final
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
(
Executors
.
defaultThreadFactory
());
private
final
ScheduledExecutorService
scheduledExecutor
=
Executors
.
newSingleThreadScheduledExecutor
();
private
final
ScheduledExecutorService
scheduledExecutor
=
Executors
.
newSingleThreadScheduledExecutor
();
private
String
mTopActivityBefore
=
""
;
private
static
final
CharSequence
CHANNEL_NAME
=
"demon-app"
;
private
static
final
String
CHANNEL_ID
=
"Daemon-1"
;
@Override
@Override
public
void
onCreate
()
{
public
void
onCreate
()
{
super
.
onCreate
();
super
.
onCreate
();
Log
.
i
(
TAG
,
"DaemonService is onCreate"
);
Log
.
i
(
TAG
,
"DaemonService is onCreate"
);
//runProcess();
scheduleCheckDaemonServiceAlive
();
scheduleCheckDaemonServiceAlive
();
}
}
@Override
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
Log
.
i
(
TAG
,
"on start command: "
);
Log
.
i
(
TAG
,
"DaemonService is onStartCommand: "
);
NotificationChannel
channel
=
null
;
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
>=
android
.
os
.
Build
.
VERSION_CODES
.
O
)
{
channel
=
new
NotificationChannel
(
CHANNEL_ID
,
CHANNEL_NAME
,
NotificationManager
.
IMPORTANCE_HIGH
);
NotificationManager
manager
=
(
NotificationManager
)
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
if
(
manager
!=
null
)
{
manager
.
createNotificationChannel
(
channel
);
}
channel
.
setSound
(
null
,
null
);
Notification
notification
=
new
Notification
.
Builder
(
getApplicationContext
(),
CHANNEL_ID
).
build
();
startForeground
(
11
,
notification
);
}
return
START_STICKY
;
return
START_STICKY
;
}
}
@Override
@Override
public
void
onDestroy
()
{
public
void
onDestroy
()
{
Log
.
i
(
TAG
,
"ODestroy"
);
Log
.
i
(
TAG
,
"ODestroy"
);
isRunning
=
false
;
}
}
public
DaemonService
()
{
public
DaemonService
()
{
...
@@ -63,43 +74,22 @@ public class DaemonService extends Service {
...
@@ -63,43 +74,22 @@ public class DaemonService extends Service {
@Override
@Override
public
void
run
()
{
public
void
run
()
{
try
{
try
{
if
(!
is
ServiceRunning
(
DaemonService
.
this
,
APP_SERVIC
E_NAME
))
{
if
(!
is
TopActivity
(
PACKAG
E_NAME
))
{
Log
.
d
(
TAG
,
"Class band daemon service is not alive, restart it by Daemon app"
);
Log
.
d
(
TAG
,
"Class band daemon service is not alive, restart it by Daemon app"
);
final
Intent
daemonIntent
=
new
Intent
();
final
Intent
daemonIntent
=
new
Intent
();
daemonIntent
.
setAction
(
APP_SERVICE_ACTION
);
daemonIntent
.
setAction
(
APP_SERVICE_ACTION
);
daemonIntent
.
setComponent
(
new
ComponentName
(
PACKAGE_NAME
,
APP_SERVICE_NAME
));
daemonIntent
.
setComponent
(
new
ComponentName
(
PACKAGE_NAME
,
APP_SERVICE_NAME
));
startService
(
daemonIntent
);
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
startForegroundService
(
daemonIntent
);
}
else
{
startService
(
daemonIntent
);
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
Log
.
e
(
TAG
,
e
.
toString
());
Log
.
e
(
TAG
,
e
.
toString
());
}
}
}
}
},
1000
,
1000
,
TimeUnit
.
MILLISECONDS
);
},
1000
,
2000
,
TimeUnit
.
MILLISECONDS
);
}
private
void
runProcess
()
{
isRunning
=
true
;
executorService
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
while
(
isRunning
)
{
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
try
{
final
Intent
daemonIntent
=
new
Intent
();
daemonIntent
.
setAction
(
APP_SERVICE_ACTION
);
daemonIntent
.
setComponent
(
new
ComponentName
(
PACKAGE_NAME
,
APP_SERVICE_NAME
));
startService
(
daemonIntent
);
}
catch
(
Exception
e
)
{
Log
.
e
(
TAG
,
e
.
toString
());
}
}
}
});
}
}
public
boolean
isTopActivity
(
String
packageName
)
{
public
boolean
isTopActivity
(
String
packageName
)
{
...
@@ -110,32 +100,24 @@ public class DaemonService extends Service {
...
@@ -110,32 +100,24 @@ public class DaemonService extends Service {
if
(
start
<
0
||
end
<
0
||
end
<=
start
)
{
if
(
start
<
0
||
end
<
0
||
end
<=
start
)
{
return
false
;
return
false
;
}
}
//Log.d(TAG, "s: " + start + " end:" + end + " result:" + result);
String
top
=
result
.
substring
(
start
,
end
);
String
top
=
result
.
substring
(
start
,
end
);
if
(
top
.
contentEquals
(
packageName
))
{
if
(
mTopActivityBefore
!=
null
&&
!
mTopActivityBefore
.
equals
(
result
))
{
Log
.
d
(
TAG
,
"top Activity Before = "
+
mTopActivityBefore
);
Log
.
d
(
TAG
,
"top Activity After = "
+
result
);
mTopActivityBefore
=
result
;
}
// 不是com.youbay.classcard 包名或者 跳转到设置“允许在其他应用上”。
if
(
topSituation
(
result
)
||
top
.
contentEquals
(
packageName
))
{
return
true
;
return
true
;
}
}
return
false
;
return
false
;
}
}
/**
//弹出授权界面默认是在顶部情况
* 判断Service是否正在运行
private
boolean
topSituation
(
String
activityName
)
{
*
if
(
activityName
.
contains
(
"AppDrawOverlaySettingsActivity"
)
||
activityName
.
contains
(
"GrantPermissionsActivity"
))
{
* @param context 上下文
return
true
;
* @param serviceName Service 类全名
* @return true 表示正在运行,false 表示没有运行
*/
public
static
boolean
isServiceRunning
(
Context
context
,
String
serviceName
)
{
ActivityManager
manager
=
(
ActivityManager
)
context
.
getSystemService
(
Context
.
ACTIVITY_SERVICE
);
List
<
ActivityManager
.
RunningServiceInfo
>
serviceInfoList
=
manager
.
getRunningServices
(
200
);
if
(
serviceInfoList
.
size
()
<=
0
)
{
return
false
;
}
for
(
ActivityManager
.
RunningServiceInfo
info
:
serviceInfoList
)
{
//Log.d(TAG,info.service.getClassName());
if
(
info
.
service
.
getClassName
().
equals
(
serviceName
))
{
return
true
;
}
}
}
return
false
;
return
false
;
}
}
...
...
app/src/main/java/com/youbay/daemon/MainActivity.java
View file @
8099d45f
package
com
.
youbay
.
daemon
;
package
com
.
youbay
.
daemon
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.content.ComponentName
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.
content.pm.PackageManager
;
import
android.
os.Build
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.util.Log
;
import
android.widget.Toast
;
import
android.widget.Toast
;
import
androidx.appcompat.app.AppCompatActivity
;
public
class
MainActivity
extends
AppCompatActivity
{
public
class
MainActivity
extends
AppCompatActivity
{
private
static
final
String
TAG
=
"MainActivity"
;
private
static
final
String
TAG
=
"MainActivity"
;
...
@@ -17,10 +16,14 @@ public class MainActivity extends AppCompatActivity {
...
@@ -17,10 +16,14 @@ public class MainActivity extends AppCompatActivity {
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
//setContentView(R.layout.activity_main);
//setContentView(R.layout.activity_main);
Toast
.
makeText
(
this
,
"守护服务启动成功"
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
this
,
"守护服务启动成功"
,
Toast
.
LENGTH_SHORT
).
show
();
Log
.
d
(
TAG
,
"守护服务启动成功"
);
Log
.
d
(
TAG
,
"守护服务启动成功"
);
startService
(
new
Intent
(
this
,
DaemonService
.
class
));
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
startForegroundService
(
new
Intent
(
this
,
DaemonService
.
class
));
}
else
{
startService
(
new
Intent
(
this
,
DaemonService
.
class
));
}
/*PackageManager packageManager = getPackageManager();
/*PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
ComponentName componentName = new ComponentName(this, MainActivity.class);
int res = packageManager.getComponentEnabledSetting(componentName);
int res = packageManager.getComponentEnabledSetting(componentName);
...
...
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