Sfoglia il codice sorgente

1.修改Android Apex Drivers推送通知在UI线程处理以及界面初始化的Context不正确。

Pen Li 7 anni fa
parent
commit
ef44e9fc9e

+ 23 - 5
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -17,7 +17,9 @@ import android.content.SharedPreferences;
 import android.location.Location;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
+import android.os.Looper;
 import android.support.v4.app.NotificationCompat;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
@@ -64,6 +66,8 @@ public class ApexDriverApplication extends Application {
     private ApexDriversBackgroundService mService;
     private boolean mRequiredLocation = false;
 
+    private Activity mCurActivity;
+
 
     private OperationQueue networkQueue;
 
@@ -90,7 +94,7 @@ public class ApexDriverApplication extends Application {
 
         @Override
         public void onActivityStarted(Activity activity) {
-
+            mCurActivity = activity;
         }
 
         @Override
@@ -522,7 +526,7 @@ public class ApexDriverApplication extends Application {
             break;
             case BackgroundReportTypeAlways: {
 
-                new AlertDialog.Builder(getApplicationContext())
+                new AlertDialog.Builder(mCurActivity)
                         .setTitle("Warning")
                         .setMessage("Report Location For Order:" + orderId)
                         .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@@ -558,7 +562,16 @@ public class ApexDriverApplication extends Application {
         }
     }
 
-    public void receiveNotification(JSONObject notification) {
+    public void receiveNotificationOnMainThread(final JSONObject notification) {
+        new Handler(Looper.getMainLooper()).post(new Runnable() {
+            @Override
+            public void run() {
+                receiveNotification(notification);
+            }
+        });
+    }
+
+    private void receiveNotification(JSONObject notification) {
 
         if (notification != null) {
 
@@ -592,7 +605,7 @@ public class ApexDriverApplication extends Application {
                                 msg = orderID + " status changed,view detail?";
                             }
 
-                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
+                            AlertDialog.Builder builder = new AlertDialog.Builder(mCurActivity);
                             builder.setTitle("Message");
                             builder.setMessage(msg);
                             if (required == 0) {
@@ -653,7 +666,11 @@ public class ApexDriverApplication extends Application {
 //        intent.putExtra("goHome",true);
         Intent intent = new Intent(getApplicationContext(),MainActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-        intent.putExtra("aps",aps.toString());
+        intent.putExtra("aps",aps.toString()); // 程序在后台的情况下,点击通知将程序唤醒到前台时,并不能取得extra
+        MainActivity mainActivity = MainActivity.currentMainActivity();
+        if (mainActivity != null) {
+            mainActivity.setAPS(aps);
+        }
 
         /**
          *
@@ -689,6 +706,7 @@ public class ApexDriverApplication extends Application {
 
         }
 
+        noti.flags |= Notification.FLAG_AUTO_CANCEL;
         nm.notify(id, noti);
     }
 

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

@@ -43,12 +43,28 @@ import com.usai.redant.rautils.utils.dbgUtil;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.lang.ref.WeakReference;
+
 
 public class MainActivity extends BasicActivity implements LoginFragment.LoginCallBack {
 
     private RelativeLayout mRootContainer;
     private final static String FragmentTag = "ContentFragmentTag";
 
+    private JSONObject mAps;
+    private static WeakReference<MainActivity> weakSelf = null;
+
+    public static MainActivity currentMainActivity() {
+        if (weakSelf != null) {
+            return weakSelf.get();
+        }
+        return null;
+    }
+
+    public void setAPS(JSONObject mAps) {
+        this.mAps = mAps;
+    }
+
     void Test()
     {
         dbgUtil.fileLog(this,"lib import successful");
@@ -129,6 +145,7 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 //            setTheme(R.style.ApexDriverThemeWhite);
 //            autoSetupStatusBar = false;
 //        }
+        weakSelf = new WeakReference<>(this);
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
@@ -143,16 +160,36 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
         checkPermissions();
 
+        checkNotificationEnable();
+        checkNotification();
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+
+        checkNotification();
+    }
+
+    /**
+     * 检查是否点击通知栏激活应用到前台
+     * */
+    private void checkNotification() {
+
         if (ApexDriverApplication.sharedApplication().isLogin()) {
 
             Intent intent = getIntent();
             if (intent != null) {
                 String apsStr = intent.getStringExtra("aps");
-                if (!TextUtils.isEmpty(apsStr)) {
 
-                    try {
-                        JSONObject aps = new JSONObject(apsStr);
+                try {
+                    JSONObject aps = mAps;
 
+                    if (aps == null && !TextUtils.isEmpty(apsStr)) {
+                        aps = new JSONObject(apsStr);
+                    }
+
+                    if (aps != null) {
                         final String orderID = aps.optString("order-id");
                         final int orderType = aps.optInt("order-type");
                         final String orderType2 = aps.optString("order-type2");
@@ -160,16 +197,16 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
                         Intent detailIntent = DetailActivity.build(getApplicationContext(),orderID,orderType,orderType2,statusNo);
                         startActivity(detailIntent);
-
-                    } catch (JSONException e) {
-                        e.printStackTrace();
                     }
 
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                } finally {
+                    mAps = null;
                 }
             }
         }
-
-        checkNotificationEnable();
+        mAps = null;
     }
 
     void checkPowerManagement()

+ 3 - 1
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/network/Network.java

@@ -399,7 +399,9 @@ public class Network extends com.usai.redant.rautils.utils.Network {
 
         prepareParams(params);
 
-        String jsonStr = getJSON(URL_UPLOAD_TOKEN,params,PUSH_TIME_OUT_INTERVAL);
+        String url = "http://192.168.0.130:8080/MyWeb/Test";
+//        String url = URL_UPLOAD_TOKEN;
+        String jsonStr = getJSON(url,params,PUSH_TIME_OUT_INTERVAL);
 
         return handleJson(jsonStr);
     }

+ 2 - 2
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/receiver/ApexDriverAlarmReceiver.java

@@ -149,7 +149,7 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
 
 
 
-    private void check_push(Context context)
+    private void check_push(final Context context)
     {
 
 
@@ -171,7 +171,7 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
                             for (int i = 0; i < notifications.length(); i++) {
                                 JSONObject notification = notifications.optJSONObject(i);
                                 if (notification != null) {
-                                    ApexDriverApplication.sharedApplication().receiveNotification(notification);
+                                    ApexDriverApplication.sharedApplication().receiveNotificationOnMainThread(notification);
                                 }
                             }