Prechádzať zdrojové kódy

Migrate from activity to service to handle the click events

Sebastián Katzer 7 rokov pred
rodič
commit
c008cc682a

+ 1 - 3
plugin.xml

@@ -115,10 +115,8 @@
                 android:name="de.appplant.cordova.plugin.localnotification.ClearReceiver"
                 android:exported="false" />
 
-            <activity
+            <service
                 android:name="de.appplant.cordova.plugin.localnotification.ClickReceiver"
-                android:launchMode="singleInstance"
-                android:theme="@android:style/Theme.Translucent"
                 android:exported="false" />
 
             <receiver

+ 3 - 3
src/android/notification/Builder.java

@@ -353,7 +353,7 @@ public final class Builder {
 
         int reqCode = random.nextInt();
 
-        PendingIntent contentIntent = PendingIntent.getActivity(
+        PendingIntent contentIntent = PendingIntent.getService(
                 context, reqCode, intent, FLAG_UPDATE_CURRENT);
 
         builder.setContentIntent(contentIntent);
@@ -403,8 +403,8 @@ public final class Builder {
 
         int reqCode = random.nextInt();
 
-        return PendingIntent.getActivity(
-                context, reqCode, intent, FLAG_CANCEL_CURRENT);
+        return PendingIntent.getService(
+                context, reqCode, intent, FLAG_UPDATE_CURRENT);
     }
 
     /**

+ 24 - 7
src/android/notification/receiver/AbstractClickReceiver.java

@@ -21,7 +21,7 @@
 
 package de.appplant.cordova.plugin.notification.receiver;
 
-import android.app.Activity;
+import android.app.IntentService;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
@@ -38,16 +38,25 @@ import static de.appplant.cordova.plugin.notification.action.Action.EXTRA_ID;
  * Abstract content receiver activity for local notifications. Creates the
  * local notification and calls the event functions for further proceeding.
  */
-abstract public class AbstractClickReceiver extends Activity {
+abstract public class AbstractClickReceiver extends IntentService {
+
+    // Holds a reference to the intent to handle.
+    private Intent intent;
+
+    public AbstractClickReceiver() {
+        super("LocalNotificationClickReceiver");
+    }
 
     /**
      * Called when local notification was clicked to launch the main intent.
      */
     @Override
-    protected void onResume() {
-        super.onResume();
+    protected void onHandleIntent(Intent intent) {
+        this.intent        = intent;
+
+        if (intent == null)
+            return;
 
-        Intent intent      = getIntent();
         Bundle bundle      = intent.getExtras();
         Context context    = getApplicationContext();
 
@@ -61,7 +70,7 @@ abstract public class AbstractClickReceiver extends Activity {
             return;
 
         onClick(toast, bundle);
-        finish();
+        this.intent = null;
     }
 
     /**
@@ -79,6 +88,13 @@ abstract public class AbstractClickReceiver extends Activity {
         return getIntent().getExtras().getString(EXTRA_ID, CLICK_ACTION_ID);
     }
 
+    /**
+     * Getter for the received intent.
+     */
+    protected Intent getIntent() {
+        return intent;
+    }
+
     /**
      * Launch main intent from package.
      */
@@ -94,7 +110,8 @@ abstract public class AbstractClickReceiver extends Activity {
             return;
 
         intent.addFlags(
-                FLAG_ACTIVITY_REORDER_TO_FRONT | FLAG_ACTIVITY_SINGLE_TOP);
+              FLAG_ACTIVITY_REORDER_TO_FRONT
+            | FLAG_ACTIVITY_SINGLE_TOP);
 
         context.startActivity(intent);
     }