Commit 8099d45f authored by 胡江林's avatar 胡江林

针对Android8.0启动服务处理,getRunningService无效处理。

parent 700e4f16
......@@ -4,7 +4,11 @@
<uses-permission android:name="app.custom.permission" />
<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
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......
......@@ -6,6 +6,7 @@ import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
/**
* @author Administrator
......@@ -34,13 +35,23 @@ public class BootReceiver extends BroadcastReceiver {
}
private void startIntent(Context context) {
if (isServiceRunning(context)) {
/*if (isServiceRunning(context)) {
System.out.println("this daemon service is running ..");
} else {
final Intent daemonIntent = new Intent();
daemonIntent.setAction(DAEMON_SERVICE_ACTION);
daemonIntent.setComponent(new ComponentName(APP_PACKAGE_NAME, DAEMON_SERVICE_NAME));
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);
}
}
......
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.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -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_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 String mTopActivityBefore = "";
private static final CharSequence CHANNEL_NAME = "demon-app";
private static final String CHANNEL_ID = "Daemon-1";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "DaemonService is onCreate");
//runProcess();
scheduleCheckDaemonServiceAlive();
}
@Override
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;
}
@Override
public void onDestroy() {
Log.i(TAG, "ODestroy");
isRunning = false;
}
public DaemonService() {
......@@ -63,43 +74,22 @@ public class DaemonService extends Service {
@Override
public void run() {
try {
if (!isServiceRunning(DaemonService.this, APP_SERVICE_NAME)) {
if (!isTopActivity(PACKAGE_NAME)) {
Log.d(TAG, "Class band daemon service is not alive, restart it by Daemon app");
final Intent daemonIntent = new Intent();
daemonIntent.setAction(APP_SERVICE_ACTION);
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) {
Log.e(TAG, e.toString());
}
}
}, 1000, 1000, 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());
}
}
}
});
}, 1000, 2000, TimeUnit.MILLISECONDS);
}
public boolean isTopActivity(String packageName) {
......@@ -110,32 +100,24 @@ public class DaemonService extends Service {
if (start < 0 || end < 0 || end <= start) {
return false;
}
//Log.d(TAG, "s: " + start + " end:" + end + " result:" + result);
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 false;
}
/**
* 判断Service是否正在运行
*
* @param context 上下文
* @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;
}
//弹出授权界面默认是在顶部情况
private boolean topSituation(String activityName) {
if (activityName.contains("AppDrawOverlaySettingsActivity") || activityName.contains("GrantPermissionsActivity")) {
return true;
}
return false;
}
......
package com.youbay.daemon;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
......@@ -17,10 +16,14 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Toast.makeText(this,"守护服务启动成功", Toast.LENGTH_SHORT).show();
Log.d(TAG,"守护服务启动成功");
startService(new Intent(this,DaemonService.class));
Toast.makeText(this, "守护服务启动成功", Toast.LENGTH_SHORT).show();
Log.d(TAG, "守护服务启动成功");
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();
ComponentName componentName = new ComponentName(this, MainActivity.class);
int res = packageManager.getComponentEnabledSetting(componentName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment