From f91ef956cf482ed4ce0885dc47794b783c20c415 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 30 四月 2025 10:14:43 +0800 Subject: [PATCH] Merge branch '1.2.0' into 1.5.1_google --- app/src/main/java/com/hdl/photovoltaic/services/ForeService.java | 136 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 136 insertions(+), 0 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/services/ForeService.java b/app/src/main/java/com/hdl/photovoltaic/services/ForeService.java new file mode 100644 index 0000000..24088e9 --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/services/ForeService.java @@ -0,0 +1,136 @@ +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.content.pm.ServiceInfo; +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"/> + * + * 鍦╫nCreate涓垱寤轰竴涓箍鎾帴鏀跺櫒锛岃瘯璇曡兘涓嶈兘鎺ユ敹鍒� 寮�鍗曟垨鑰呴绾︾粨鏉熷悗鐨勯�氱煡 + */ +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()); +// try { +// // Kotlin 绀轰緥 +// if (Build.VERSION.SDK_INT >= 34) { +// startForeground(1, getNotification(getString(R.string.app_name), "Running"), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); +// } else { +// startForeground(1, getNotification(getString(R.string.app_name), "Running")); +// } +// } catch (Exception e) { +// } + } + + @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; + } +} + -- Gitblit v1.8.0