From f309af7d44e74b2ff67242e3ef01a35fc30c88e0 Mon Sep 17 00:00:00 2001 From: wenyifan Date: Thu, 19 Jan 2023 09:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=88=B0Android=206.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 +- .../main/java/run/evan/gost/GostService.java | 39 +++++++++++-------- .../main/java/run/evan/gost/MainActivity.java | 6 ++- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 675fe71..21f012e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { applicationId "run.evan.gost" - minSdk 26 + minSdk 23 targetSdk 32 versionCode 1 versionName "1.0" diff --git a/app/src/main/java/run/evan/gost/GostService.java b/app/src/main/java/run/evan/gost/GostService.java index aaf0032..738b660 100644 --- a/app/src/main/java/run/evan/gost/GostService.java +++ b/app/src/main/java/run/evan/gost/GostService.java @@ -25,25 +25,30 @@ public class GostService extends Service { @Override public void onCreate() { super.onCreate(); - NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - NotificationChannel Channel = new NotificationChannel(CHANNEL_ID, "GOST", NotificationManager.IMPORTANCE_HIGH); - Channel.enableLights(false); - Channel.setLightColor(Color.RED); - Channel.setShowBadge(true); - Channel.setDescription( "GOST"); - Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - manager.createNotificationChannel(Channel); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + NotificationChannel Channel = null; - Notification notification = new Notification.Builder(this) - .setChannelId(CHANNEL_ID) - .setContentTitle("GOST")//标题 - .setContentText(getResources().getString(R.string.service_text))//内容 + Channel = new NotificationChannel(CHANNEL_ID, "GOST", NotificationManager.IMPORTANCE_HIGH); + + Channel.enableLights(false); + Channel.setLightColor(Color.RED); + Channel.setShowBadge(true); + Channel.setDescription("GOST"); + Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + manager.createNotificationChannel(Channel); + } + Notification.Builder builder = new Notification.Builder(this); + builder = builder.setContentTitle("GOST") + .setContentText(getResources().getString(R.string.service_text)) .setWhen(System.currentTimeMillis()) - .setSmallIcon(R.mipmap.ic_launcher)//小图标一定需要设置,否则会报错(如果不设置它启动服务前台化不会报错,但是你会发现这个通知不会启动),如果是普通通知,不设置必然报错 - .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) - .build(); - startForeground(1, notification);//服务前台化只能使用startForeground()方法,不能使用 notificationManager.notify(1,notification); 这个只是启动通知使用的,使用这个方法你只需要等待几秒就会发现报错了 - + .setSmallIcon(R.mipmap.ic_launcher) + .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + builder = builder.setChannelId(CHANNEL_ID); + } + Notification notification = builder.build(); + startForeground(1, notification); } } diff --git a/app/src/main/java/run/evan/gost/MainActivity.java b/app/src/main/java/run/evan/gost/MainActivity.java index 87ffe9f..7169417 100644 --- a/app/src/main/java/run/evan/gost/MainActivity.java +++ b/app/src/main/java/run/evan/gost/MainActivity.java @@ -90,7 +90,11 @@ public class MainActivity extends AppCompatActivity { case 2: activity.btnStart.setText(R.string.btn_stop); activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-1499549)); - activity.getApplicationContext().startForegroundService(activity.gostServiceIntent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + activity.getApplicationContext().startForegroundService(activity.gostServiceIntent); + }else { + activity.getApplicationContext().startService(activity.gostServiceIntent); + } break; case 3: activity.btnStart.setText(R.string.btn_start);