OSDN Git Service

Let NotificationService handle starting services from broadcast receivers
authorBraden Farmer <farmerbb@gmail.com>
Tue, 17 Oct 2017 04:21:38 +0000 (22:21 -0600)
committerBraden Farmer <farmerbb@gmail.com>
Tue, 17 Oct 2017 04:21:38 +0000 (22:21 -0600)
app/src/main/java/com/farmerbb/taskbar/receiver/BootReceiver.java
app/src/main/java/com/farmerbb/taskbar/receiver/PackageUpgradeReceiver.java
app/src/main/java/com/farmerbb/taskbar/receiver/ShowHideTaskbarReceiver.java
app/src/main/java/com/farmerbb/taskbar/receiver/StartReceiver.java
app/src/main/java/com/farmerbb/taskbar/service/NotificationService.java

index 633ec1c..78a2db2 100644 (file)
@@ -20,13 +20,9 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Build;
-import android.os.Handler;
 
 import com.farmerbb.taskbar.activity.DummyActivity;
-import com.farmerbb.taskbar.service.DashboardService;
 import com.farmerbb.taskbar.service.NotificationService;
-import com.farmerbb.taskbar.service.StartMenuService;
-import com.farmerbb.taskbar.service.TaskbarService;
 import com.farmerbb.taskbar.util.U;
 
 public class BootReceiver extends BroadcastReceiver {
@@ -45,6 +41,8 @@ public class BootReceiver extends BroadcastReceiver {
                 editor.putLong("time_of_service_start", System.currentTimeMillis());
                 editor.apply();
 
+                boolean startServices = false;
+
                 if(!pref.getBoolean("is_hidden", false)) {
                     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
                         Intent intent2 = new Intent(context, DummyActivity.class);
@@ -54,17 +52,16 @@ public class BootReceiver extends BroadcastReceiver {
                         context.startActivity(intent2);
                     }
 
-                    new Handler().postDelayed(() -> {
-                        context.startService(new Intent(context, TaskbarService.class));
-                        context.startService(new Intent(context, StartMenuService.class));
-                        context.startService(new Intent(context, DashboardService.class));
-                    }, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : 100);
+                    startServices = true;
                 }
 
+                Intent notificationIntent = new Intent(context, NotificationService.class);
+                notificationIntent.putExtra("start_services", startServices);
+
                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
-                    context.startForegroundService(new Intent(context, NotificationService.class));
+                    context.startForegroundService(notificationIntent);
                 else
-                    context.startService(new Intent(context, NotificationService.class));
+                    context.startService(notificationIntent);
             } else {
                 editor.putBoolean("taskbar_active", U.isServiceRunning(context, NotificationService.class));
                 editor.apply();
index 21fd034..1eec3b8 100644 (file)
@@ -20,13 +20,9 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Build;
-import android.os.Handler;
 
 import com.farmerbb.taskbar.activity.DummyActivity;
-import com.farmerbb.taskbar.service.DashboardService;
 import com.farmerbb.taskbar.service.NotificationService;
-import com.farmerbb.taskbar.service.StartMenuService;
-import com.farmerbb.taskbar.service.TaskbarService;
 import com.farmerbb.taskbar.util.U;
 
 public class PackageUpgradeReceiver extends BroadcastReceiver {
@@ -34,6 +30,8 @@ public class PackageUpgradeReceiver extends BroadcastReceiver {
     public void onReceive(Context context, Intent intent) {
         if(intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) {
             SharedPreferences pref = U.getSharedPreferences(context);
+            boolean startServices = false;
+
             if(pref.getBoolean("taskbar_active", false) && !pref.getBoolean("is_hidden", false)) {
                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
                     Intent intent2 = new Intent(context, DummyActivity.class);
@@ -43,18 +41,17 @@ public class PackageUpgradeReceiver extends BroadcastReceiver {
                     context.startActivity(intent2);
                 }
 
-                new Handler().postDelayed(() -> {
-                    context.startService(new Intent(context, TaskbarService.class));
-                    context.startService(new Intent(context, StartMenuService.class));
-                    context.startService(new Intent(context, DashboardService.class));
-                }, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : 100);
+                startServices = true;
             }
 
             if(pref.getBoolean("taskbar_active", false)) {
+                Intent notificationIntent = new Intent(context, NotificationService.class);
+                notificationIntent.putExtra("start_services", startServices);
+
                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
-                    context.startForegroundService(new Intent(context, NotificationService.class));
+                    context.startForegroundService(notificationIntent);
                 else
-                    context.startService(new Intent(context, NotificationService.class));
+                    context.startService(notificationIntent);
             }
         }
     }
index 2ffe0fa..dffc24b 100644 (file)
@@ -20,7 +20,6 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Build;
-import android.os.Handler;
 import android.support.v4.content.LocalBroadcastManager;
 
 import com.farmerbb.taskbar.activity.DummyActivity;
@@ -54,11 +53,7 @@ public class ShowHideTaskbarReceiver extends BroadcastReceiver {
                 context.startActivity(intent2);
             }
 
-            new Handler().postDelayed(() -> {
-                context.startService(taskbarIntent);
-                context.startService(startMenuIntent);
-                context.startService(dashboardIntent);
-            }, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : 100);
+            notificationIntent.putExtra("start_services", true);
 
             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                 context.startForegroundService(notificationIntent);
index 5689ed8..23a8f3b 100644 (file)
@@ -20,13 +20,9 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Build;
-import android.os.Handler;
 
 import com.farmerbb.taskbar.activity.DummyActivity;
-import com.farmerbb.taskbar.service.DashboardService;
 import com.farmerbb.taskbar.service.NotificationService;
-import com.farmerbb.taskbar.service.StartMenuService;
-import com.farmerbb.taskbar.service.TaskbarService;
 import com.farmerbb.taskbar.util.U;
 
 public class StartReceiver extends BroadcastReceiver {
@@ -64,16 +60,13 @@ public class StartReceiver extends BroadcastReceiver {
                 context.startActivity(intent2);
             }
 
-            new Handler().postDelayed(() -> {
-                context.startService(new Intent(context, TaskbarService.class));
-                context.startService(new Intent(context, StartMenuService.class));
-                context.startService(new Intent(context, DashboardService.class));
-            }, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? 0 : 100);
+            Intent notificationIntent = new Intent(context, NotificationService.class);
+            notificationIntent.putExtra("start_services", true);
 
             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
-                context.startForegroundService(new Intent(context, NotificationService.class));
+                context.startForegroundService(notificationIntent);
             else
-                context.startService(new Intent(context, NotificationService.class));
+                context.startService(notificationIntent);
         }
     }
 }
index 093f8df..9309407 100644 (file)
@@ -52,6 +52,12 @@ public class NotificationService extends Service {
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
+        if(intent != null && intent.getBooleanExtra("start_services", false)) {
+            startService(new Intent(this, TaskbarService.class));
+            startService(new Intent(this, StartMenuService.class));
+            startService(new Intent(this, DashboardService.class));
+        }
+
         return START_STICKY;
     }