|
|
@@ -0,0 +1,187 @@
|
|
|
+package com.sheep.jiuyan.samllsheep.service;
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.app.Notification;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.app.Service;
|
|
|
+import android.app.usage.UsageStats;
|
|
|
+import android.app.usage.UsageStatsManager;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.widget.RemoteViews;
|
|
|
+
|
|
|
+import com.sheep.gamegroup.util.ListUtil;
|
|
|
+import com.sheep.gamegroup.util.LogUtil;
|
|
|
+import com.sheep.gamegroup.util.RunningTaskUtil;
|
|
|
+import com.sheep.gamegroup.util.ScreenShotListenManager;
|
|
|
+import com.sheep.gamegroup.view.activity.ActMain;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.FileUtil;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import rx.Observable;
|
|
|
+import rx.android.schedulers.AndroidSchedulers;
|
|
|
+import rx.functions.Action1;
|
|
|
+import rx.schedulers.Schedulers;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2018/9/20.
|
|
|
+ * realicing@sina.com
|
|
|
+ */
|
|
|
+public class ListenerShotService extends Service {
|
|
|
+
|
|
|
+ private RunningTaskUtil runningTaskUtil;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
+ runningTaskUtil = new RunningTaskUtil(getApplicationContext());
|
|
|
+ manager = ScreenShotListenManager.newInstance(getApplicationContext());
|
|
|
+ manager.setListener(
|
|
|
+ new ScreenShotListenManager.OnScreenShotListener() {
|
|
|
+ public void onShot(String imagePath) {
|
|
|
+ addPng(imagePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ manager.startListen();
|
|
|
+
|
|
|
+ Notification notification = new Notification();
|
|
|
+ //通知栏没有展开时的显示内容
|
|
|
+ notification.icon = R.mipmap.icon;
|
|
|
+ notification.tickerText = "回到小绵羊";
|
|
|
+
|
|
|
+ //下拉通知栏的显示内容
|
|
|
+ notification.contentView = new RemoteViews(getPackageName() , R.layout.notification_sheep_shot_screen);
|
|
|
+
|
|
|
+ //点击通知栏跳转到相应的应用里面
|
|
|
+ Intent intent = new Intent(this , ActMain.class);
|
|
|
+ //这一句加不加没什么影响
|
|
|
+// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ notification.contentIntent = PendingIntent.getActivity(this , 1 , intent , 0);
|
|
|
+
|
|
|
+ //这里的id不能是0
|
|
|
+ startForeground(1 , notification);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ private ScreenShotListenManager manager;
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (manager != null)
|
|
|
+ manager.stopListen();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前台运行的程序的包名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SuppressLint("NewApi")
|
|
|
+ public String getPackageNameInForeground1(Context context) {
|
|
|
+ String topPackageName = runningTaskUtil.getTopRunningTasks();
|
|
|
+ if(TextUtils.isEmpty(topPackageName)){
|
|
|
+ return getAppProcessInfoInForeground(context);
|
|
|
+ }
|
|
|
+ return topPackageName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前台运行的程序的包名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SuppressLint("NewApi")
|
|
|
+ public static String getPackageNameInForeground(Context context) {
|
|
|
+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);//usagestats
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
+ if (mUsageStatsManager != null) {
|
|
|
+ List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, time-5000L, time);
|
|
|
+
|
|
|
+ if (!ListUtil.isEmpty(usageStatsList)) {
|
|
|
+ TreeMap<Long, UsageStats> mySortedMap = new TreeMap<>();
|
|
|
+ for (UsageStats item : usageStatsList) {
|
|
|
+ mySortedMap.put(item.getLastTimeUsed(), item);
|
|
|
+ }
|
|
|
+
|
|
|
+ UsageStats usageStats = mySortedMap.get(mySortedMap.lastKey());
|
|
|
+
|
|
|
+ return usageStats.getPackageName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return getAppProcessInfoInForeground(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getAppProcessInfoInForeground(Context context) {
|
|
|
+ android.app.ActivityManager manager = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
+ List<android.app.ActivityManager.RunningAppProcessInfo> processList = manager == null ? null : manager.getRunningAppProcesses();
|
|
|
+ if (!ListUtil.isEmpty(processList)) {
|
|
|
+ for (android.app.ActivityManager.RunningAppProcessInfo item : processList) {
|
|
|
+ if (item != null && item.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
|
|
|
+ return item.processName;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addPng(final String imagePath) {
|
|
|
+ final String packageName = getPackageNameInForeground1(getApplicationContext());
|
|
|
+ if(TextUtils.isEmpty(packageName)){
|
|
|
+ LogUtil.println("ScreenShotListenManager", "无法获取到当前运行的应用的包名");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+// File shotFile = getScreenShotsFile(packageName);
|
|
|
+ LogUtil.println("ScreenShotListenManager", packageName);
|
|
|
+ Observable.just(1).delay(1, TimeUnit.SECONDS)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new Action1<Integer>() {
|
|
|
+ @Override
|
|
|
+ public void call(Integer integer) {
|
|
|
+ try {
|
|
|
+ FileUtil.copyFileToDir(new File(imagePath), getScreenShotsDir(getApplicationContext(), packageName));
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ LogUtil.println("ScreenShotListenManager", "复制截屏图片失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 截图目录
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static File getScreenShotsDir(Context context) {
|
|
|
+ return context.getDir("ScreenShots", Context.MODE_PRIVATE);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 指定程序的截图目录
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static File getScreenShotsDir(Context context, String packageName) {
|
|
|
+ File dir = new File(getScreenShotsDir(context), packageName);
|
|
|
+ if(!dir.exists())
|
|
|
+ dir.mkdirs();
|
|
|
+ return dir;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|