app/src/main/AndroidManifest.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/hdl/photovoltaic/HDLApp.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/hdl/photovoltaic/services/ForeService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/hdl/photovoltaic/ui/CPowerStationActivity.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
app/src/main/AndroidManifest.xml
@@ -56,6 +56,8 @@ <uses-permission android:name="com.hdl.photovoltaic.permission.JPUSH_MESSAGE" /> <!-- 适配Android13,弹出通知必须权限 --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- 前台服务权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <queries> <package android:name="com.hdl.photovoltaic.services" /> @@ -230,6 +232,11 @@ </intent-filter> </service> <service android:name=".services.ForeService" android:enabled="true" android:exported="true" /> <meta-data android:name="design_height_in_dp" android:value="812" /> app/src/main/java/com/hdl/photovoltaic/HDLApp.java
@@ -7,6 +7,7 @@ import android.content.res.Configuration; import android.os.Build; import android.os.LocaleList; import android.os.Process; import android.os.SystemClock; import android.text.TextUtils; import android.util.Log; @@ -60,6 +61,7 @@ public class HDLApp extends Application { final String TAG=HDLApp.class.getName(); //控制打印 Debug模式打印 private Boolean isLogEnabled = true; //三方sdk初始化标识 @@ -85,7 +87,12 @@ public void onCreate() { super.onCreate(); // 获取当前进程的PID Log.d("进程===", android.os.Process.myPid() + ""); String processMessage= "进程ID:"+Process.myPid()+" 进程名:"+RuningAcitvityUtil.getAppName(getBaseContext()); Log.d(TAG, processMessage); if (RuningAcitvityUtil.getAppName(getBaseContext()).contains(":")) { //非原生进程,用初始化后面的 return; } mHDLApp = this; SharedPreUtils.init(this); getAppLanguage(); app/src/main/java/com/hdl/photovoltaic/services/ForeService.java
New file @@ -0,0 +1,125 @@ package com.hdl.photovoltaic.services; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.os.IBinder; import android.text.TextUtils; import android.util.Log; import static android.app.Notification.VISIBILITY_SECRET; import androidx.core.app.NotificationCompat; import com.hdl.photovoltaic.R; /*** * @Description: 前台服务 * channelId必须要一致,否则会报 android.app.RemoteServiceException: Bad notification for startForeground 错误 * 8.0之上一定要使用 NotificationChannel 适配下才行 * 步骤 * 1.通过 “通知服务” 创建 NotificationChannel * 2.通过 Notification.Builder 构造器 创建 Notification * 3.通过 startForeground 开启服务 * 4.高于9.0的版本 manifest需要增加 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> * * 在onCreate中创建一个广播接收器,试试能不能接收到 开单或者预约结束后的通知 */ public class ForeService extends Service { final String TAG=ForeService.class.getName(); @Override public IBinder onBind(Intent intent) { return null; } @SuppressLint("ForegroundServiceType") @Override public void onCreate() { super.onCreate(); Log.d(TAG, "ForeService onCreate() 进程Id:"+android.os.Process.myPid() ); startForeground(1, getNotification(getString(R.string.app_name), "Running")); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); //停止的时候销毁前台服务 stopForeground(true); } private Notification getNotification(String title, String message) { createNotificationChannel(); //创建一个通知消息的构造器 Notification.Builder builder = new Notification.Builder(this); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { //Android8.0开始必须给每个通知分配对应的渠道 builder = new Notification.Builder(this, "f_channel_id"); } builder.setAutoCancel(true)//设置是否允许自动清除 .setSmallIcon(R.drawable.logo)//设置状态栏里的小图标 .setContentTitle(title)//设置通知栏里面的标题文本 .setContentText(message);//设置通知栏里面的内容文本 //根据消息构造器创建一个通知对象 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { return builder.build(); } return null; } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel() { NotificationChannel channel = new NotificationChannel("f_channel_id", "CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH); //是否绕过请勿打扰模式 channel.canBypassDnd(); //闪光灯 channel.enableLights(true); //锁屏显示通知 channel.setLockscreenVisibility(VISIBILITY_SECRET); //闪关灯的灯光颜色 channel.setLightColor(Color.RED); //桌面launcher的消息角标 channel.canShowBadge(); //是否允许震动 channel.enableVibration(false); //获取系统通知响铃声音的配置 channel.getAudioAttributes(); //获取通知取到组 channel.getGroup(); //设置可绕过 请勿打扰模式 channel.setBypassDnd(true); //设置震动模式 channel.setVibrationPattern(new long[]{100, 100, 200}); //是否会有灯光 channel.shouldShowLights(); getManager().createNotificationChannel(channel); } private NotificationManager mManager; private NotificationManager getManager() { if (mManager == null) { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } return mManager; } } app/src/main/java/com/hdl/photovoltaic/ui/CPowerStationActivity.java
@@ -1,6 +1,8 @@ package com.hdl.photovoltaic.ui; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Process; import android.os.SystemClock; @@ -29,6 +31,7 @@ import com.hdl.photovoltaic.other.HdlResidenceLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; import com.hdl.photovoltaic.other.HdlUniLogic; import com.hdl.photovoltaic.services.ForeService; import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; import com.hdl.photovoltaic.ui.bean.HouseIdBean; import com.hdl.photovoltaic.ui.bean.OidBean; @@ -83,6 +86,18 @@ this.pushTokens(); //获取云端脚本 HdlESLocalJsonLogic.getInstance().getAllHdlESLocalJson(); mForegroundService= new Intent(this, ForeService.class); startService(); } Intent mForegroundService; private void startService(){ // Android 8.0使用startForegroundService在前台启动新服务 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ startForegroundService(mForegroundService); } else { startService(mForegroundService); } } @Override @@ -308,6 +323,7 @@ HdlUniLogic.getInstance().checkRemoveOtherUniMPEventCallBack(); //移除监听 HDLLinkLocalSdk.getInstance().removeAllTopicsListener(allTopicsListener); stopService(mForegroundService); } /** app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java
@@ -2,8 +2,11 @@ import android.app.Application; import android.content.Context; import android.os.Build; import android.os.Environment; import android.os.Process; import android.text.TextUtils; import android.util.Log; import com.google.gson.Gson; @@ -25,6 +28,7 @@ import java.util.HashMap; import io.dcloud.common.DHInterface.ICallBack; import io.dcloud.common.util.RuningAcitvityUtil; import io.dcloud.feature.sdk.DCSDKInitConfig; import io.dcloud.feature.sdk.DCUniMPSDK; import io.dcloud.feature.sdk.Interface.IDCUniMPPreInitCallback; @@ -134,6 +138,8 @@ public void onUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback) { // handelUniMPEventReceive(appid, event, data, callback); HdlLogLogic.print("收到小程序通知 event=" + event); String processMessage=Process.myPid()+" "; HdlLogLogic.print("进程信息:" + processMessage); //暂时通过这个处理小程序同时多条相同的请求 if (data == null) { HdlLogLogic.print("收到小程序通知,data数据为null。", true);