兼容到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 {
applicationId "run.evan.gost"
minSdk 26
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"

View File

@ -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);
}
}

View File

@ -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);