Prechádzať zdrojové kódy

1.修改Android Apex Drivers登陆成功后程序Crash。
2.修改Android Apex Drivers收到通知时响铃、震动以及闪灯。

Pen Li 7 rokov pred
rodič
commit
010fc8f523

+ 30 - 1
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -13,11 +13,15 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.location.Location;
+import android.media.AudioAttributes;
+import android.media.RingtoneManager;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Looper;
+import android.provider.Settings;
 import android.support.v4.app.NotificationCompat;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
@@ -35,6 +39,7 @@ import org.json.JSONObject;
 
 import java.lang.reflect.Field;
 
+import static android.app.Notification.VISIBILITY_PUBLIC;
 import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
 
 public class ApexDriverApplication extends Application {
@@ -598,7 +603,7 @@ public class ApexDriverApplication extends Application {
                         sendBroadcast(new Intent(HomeFragment.HomeReloadBroadcastAction));
 
                         boolean isActive = !isbackground; // 程序是否在前台
-                        if (isActive) {
+                         if (isActive) {
                             // 弹窗提示
 
                             final int orderType = aps.optInt("order-type");
@@ -688,10 +693,22 @@ public class ApexDriverApplication extends Application {
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 
         Notification noti = null;
+        long[] vibrates = { 0, 1000, 1000, 1000 };
+
+//        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+        Uri soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
 
             NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
+            // 震动
+            channel.enableVibration(true);
+            channel.setVibrationPattern(vibrates);
+
+            channel.enableLights(true);
+
+            channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
+
             nm.createNotificationChannel(channel);
 
             noti = new NotificationCompat.Builder(this, CHANNEL_ID)
@@ -699,6 +716,9 @@ public class ApexDriverApplication extends Application {
                     .setContentText(msg)
                     .setSmallIcon(R.drawable.icon_50)
                     .setContentIntent(contentIntent)
+                    .setVisibility(VISIBILITY_PUBLIC)
+                    .setSound(soundUri)
+                    .setLights(0xff00bbff,500,200)
                     .build();
         } else {
 
@@ -707,10 +727,19 @@ public class ApexDriverApplication extends Application {
                     .setContentText(msg)
                     .setSmallIcon(R.drawable.icon_50)
                     .setContentIntent(contentIntent)
+                    .setVisibility(VISIBILITY_PUBLIC)
+                    .setSound(soundUri)
+                    .setLights(0xff00bbff,500,20)
                     .build();
 
         }
 
+        /**
+         * vibrate属性是一个长整型的数组,用于设置手机静止和振动的时长,以毫秒为单位。
+         * 参数中下标为0的值表示手机静止的时长,下标为1的值表示手机振动的时长, 下标为2的值又表示手机静止的时长,以此类推。
+         */
+        noti.vibrate = vibrates;
+
         noti.flags |= Notification.FLAG_AUTO_CANCEL;
         nm.notify(id, noti);
     }

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

@@ -436,7 +436,12 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 //        restart();
 
         // notification token
-        Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
+            }
+        }).start();
 
     }