兼容到Android 6.0

This commit is contained in:
wenyifan 2023-01-19 09:15:31 +08:00
parent f07336971b
commit f309af7d44
3 changed files with 28 additions and 19 deletions

View File

@ -8,7 +8,7 @@ android {
defaultConfig { defaultConfig {
applicationId "run.evan.gost" applicationId "run.evan.gost"
minSdk 26 minSdk 23
targetSdk 32 targetSdk 32
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"

View File

@ -25,25 +25,30 @@ public class GostService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel Channel = new NotificationChannel(CHANNEL_ID, "GOST", NotificationManager.IMPORTANCE_HIGH); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Channel.enableLights(false); NotificationChannel Channel = null;
Channel.setLightColor(Color.RED);
Channel.setShowBadge(true);
Channel.setDescription( "GOST");
Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(Channel);
Notification notification = new Notification.Builder(this) Channel = new NotificationChannel(CHANNEL_ID, "GOST", NotificationManager.IMPORTANCE_HIGH);
.setChannelId(CHANNEL_ID)
.setContentTitle("GOST")//标题 Channel.enableLights(false);
.setContentText(getResources().getString(R.string.service_text))//内容 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()) .setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)//小图标一定需要设置,否则会报错(如果不设置它启动服务前台化不会报错,但是你会发现这个通知不会启动),如果是普通通知,不设置必然报错 .setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
.build(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForeground(1, notification);//服务前台化只能使用startForeground()方法,不能使用 notificationManager.notify(1,notification); 这个只是启动通知使用的,使用这个方法你只需要等待几秒就会发现报错了 builder = builder.setChannelId(CHANNEL_ID);
}
Notification notification = builder.build();
startForeground(1, notification);
} }
} }

View File

@ -90,7 +90,11 @@ public class MainActivity extends AppCompatActivity {
case 2: case 2:
activity.btnStart.setText(R.string.btn_stop); activity.btnStart.setText(R.string.btn_stop);
activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-1499549)); 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; break;
case 3: case 3:
activity.btnStart.setText(R.string.btn_start); activity.btnStart.setText(R.string.btn_start);