Explorar el Código

尝试修复bugly上的报错

zengjiebin hace 7 años
padre
commit
3e3dced8d7

+ 17 - 16
app/src/main/java/com/sheep/jiuyan/samllsheep/service/ListenerShotNewService.java

@@ -63,22 +63,6 @@ public class ListenerShotNewService extends Service {
         if (intent != null) {
             userId = intent.getStringExtra(EXTRA_KEY_USER_ID);
         }
-        if(runningTaskUtil == null) {
-            //初始化获取当前应用的工具类
-            runningTaskUtil = new RunningTaskUtil(getApplicationContext());
-        }
-        if(manager == null) {
-            //开启监听
-            manager = ScreenShotListenManager.newInstance(getApplicationContext());
-            manager.setListener(
-                    new ScreenShotListenManager.OnScreenShotListener() {
-                        public void onShot(String imagePath) {
-                            addPng(imagePath);
-                        }
-                    }
-            );
-            manager.startListen();
-        }
 
 
         int msgId = 113922;
@@ -110,7 +94,24 @@ public class ListenerShotNewService extends Service {
                 .build();
         notification.flags |= Notification.FLAG_NO_CLEAR;
         //这里的id不能是0
+        // android 8.0 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数
         startForeground(msgId, notification);
+        if(runningTaskUtil == null) {
+            //初始化获取当前应用的工具类
+            runningTaskUtil = new RunningTaskUtil(getApplicationContext());
+        }
+        if(manager == null) {
+            //开启监听
+            manager = ScreenShotListenManager.newInstance(getApplicationContext());
+            manager.setListener(
+                    new ScreenShotListenManager.OnScreenShotListener() {
+                        public void onShot(String imagePath) {
+                            addPng(imagePath);
+                        }
+                    }
+            );
+            manager.startListen();
+        }
         return super.onStartCommand(intent, flags, startId);
     }
     private ScreenShotListenManager manager;

+ 16 - 16
app/src/main/java/com/sheep/jiuyan/samllsheep/service/ListenerShotService.java

@@ -60,22 +60,6 @@ public class ListenerShotService extends Service {
         if (intent != null) {
             userId = intent.getStringExtra(EXTRA_KEY_USER_ID);
         }
-        if(runningTaskUtil == null) {
-            //初始化获取当前应用的工具类
-            runningTaskUtil = new RunningTaskUtil(getApplicationContext());
-        }
-        if(manager == null) {
-            //开启监听
-            manager = ScreenShotListenManager.newInstance(getApplicationContext());
-            manager.setListener(
-                    new ScreenShotListenManager.OnScreenShotListener() {
-                        public void onShot(String imagePath) {
-                            addPng(imagePath);
-                        }
-                    }
-            );
-            manager.startListen();
-        }
 
         Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext());
         //点击通知栏跳转到相应的应用里面
@@ -98,6 +82,22 @@ public class ListenerShotService extends Service {
         notification.flags |= Notification.FLAG_NO_CLEAR;
         //这里的id不能是0
         startForeground(113922, notification);
+        if(runningTaskUtil == null) {
+            //初始化获取当前应用的工具类
+            runningTaskUtil = new RunningTaskUtil(getApplicationContext());
+        }
+        if(manager == null) {
+            //开启监听
+            manager = ScreenShotListenManager.newInstance(getApplicationContext());
+            manager.setListener(
+                    new ScreenShotListenManager.OnScreenShotListener() {
+                        public void onShot(String imagePath) {
+                            addPng(imagePath);
+                        }
+                    }
+            );
+            manager.startListen();
+        }
         return super.onStartCommand(intent, flags, startId);
     }
     private ScreenShotListenManager manager;

+ 12 - 8
app/src/main/java/com/sheep/jiuyan/samllsheep/ui/view/EggView.java

@@ -109,16 +109,20 @@ public class EggView extends View {
 
     //退出时销毁
     public void onDestroy() {
-        for (int i = 0; i < eggs.length; i++) {
-            if (eggs[i] != null) {
-                eggs[i].destroy();
+        if(eggs != null) {
+            for (int i = 0; i < eggs.length; i++) {
+                if (eggs[i] != null) {
+                    eggs[i].destroy();
+                }
             }
         }
-        for (Bitmap item : bitmaps) {
-            try {
-                item.recycle();
-            } catch (Exception e) {
-                e.printStackTrace();
+        if(bitmaps != null) {
+            for (Bitmap item : bitmaps) {
+                try {
+                    item.recycle();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
             }
         }
     }

+ 4 - 2
app/src/main/java/com/sheep/jiuyan/samllsheep/utils/TitleBarUtils.java

@@ -58,8 +58,10 @@ public class TitleBarUtils {
      */
     public TitleBarUtils setTitle(Activity activity, String title) {
         TextView titleView = activity.getWindow().getDecorView().findViewById(R.id.txt_baseactivity_title);
-        titleView.setVisibility(View.VISIBLE);
-        titleView.setText(title == null ? "" : title);
+        if(titleView != null) {
+            titleView.setVisibility(View.VISIBLE);
+            titleView.setText(title == null ? "" : title);
+        }
         return mTitleBarUtils;
     }