Parcourir la source

1.修改Android Apex Drivers启动App获取最新的通知并提示。

Pen Li il y a 7 ans
Parent
commit
94c7df50e3

+ 1 - 1
ApexDrivers/app/build.gradle

@@ -5,7 +5,7 @@ android {
     defaultConfig {
         applicationId "com.usai.redant.apexdrivers"
 
-        minSdkVersion 21
+        minSdkVersion 23
         targetSdkVersion 26
 
         versionCode 1

+ 45 - 6
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -3,7 +3,9 @@ package com.usai.redant.apexdrivers;
 
 import android.Manifest;
 import android.app.AlertDialog;
+import android.app.Notification;
 import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -41,6 +43,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.lang.ref.WeakReference;
+import java.util.Iterator;
 
 //import android.location.Location;
 
@@ -183,10 +186,11 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
             Intent intent = getIntent();
             String apsStr = null;
-
+            boolean is_alert = false;
             // 从Intent获取aps
             if (intent != null) {
 
+                is_alert = intent.getBooleanExtra("is_alert",false);
                 apsStr = intent.getStringExtra("aps");
 
             }
@@ -199,21 +203,56 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
                 try {
 
                     JSONObject aps = new JSONObject(apsStr);
-                    showDetailByNotification(aps);
+                    if (is_alert) {
+                        showDetailByAsk(aps);
+                    } else {
+                        showDetailByNotification(aps);
+                    }
 
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
 
+                cancelAllNotification();
+
                 return;
             }
 
-            // 如果aps为空,则点击appicon进入程序
-//            aps = getLastNotification();
-//            removeLastNotification();
-//            showDetailByAsk(aps);
+            // 如果aps为空,则点击appicon进入程序,从通知列表中取出最后一个发送,并且清除所有通知
+            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+
+            /**
+             * Recover a list of active notifications: ones that have been posted by the calling app that
+             * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app.
+             */
+            StatusBarNotification notifications[] = nm.getActiveNotifications();
+//            for (int i = notifications.length - 1; i >= 0; i--) {
+//                StatusBarNotification notification = notifications[i];
+//
+//            }
+            if (notifications.length > 0) {
+                StatusBarNotification statusBarNotification = notifications[0];
+                Notification notification = statusBarNotification.getNotification();
+                try {
+                    Intent immutIntent = new Intent();
+                    immutIntent.putExtra("is_alert",true);
+                    notification.contentIntent.send(this,statusBarNotification.getId(),immutIntent);
+                } catch (PendingIntent.CanceledException e) {
+                    e.printStackTrace();
+                }
+            }
+
         }
+    }
+
+    private void cancelAllNotification() {
+        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+        StatusBarNotification notifications[] = nm.getActiveNotifications();
 
+        for (StatusBarNotification statusBarNotification : notifications) {
+            int id = statusBarNotification.getId();
+            nm.cancel(id);
+        }
     }
 
     private void showDetailByNotification(JSONObject aps) {