android sdk适配android 15

This commit is contained in:
flash 2025-06-09 12:19:24 +08:00
parent 4a68b47fdb
commit 3634392e7f
13 changed files with 111 additions and 136 deletions

View File

@ -65,5 +65,5 @@ CIM采用业内主流开源技术构建易于扩展和使用并完美支
android端sdk引用
```
implementation "com.farsunset:cim-android-sdk:4.2.13"
implementation "com.farsunset:cim-android-sdk:4.2.15"
```

View File

@ -6,7 +6,7 @@
<groupId>com.farsunset</groupId>
<artifactId>cim-android-sdk</artifactId>
<version>4.2.13</version>
<version>4.2.15</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@ -29,7 +29,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<google.protobuf.version>3.22.0</google.protobuf.version>
<google.protobuf.version>3.25.4</google.protobuf.version>
</properties>
<dependencies>
@ -45,7 +45,7 @@
<groupId>android</groupId>
<artifactId>android</artifactId>
<scope>system</scope>
<version>10.0.0</version>
<version>15.0.0</version>
<systemPath>${project.basedir}/libs/android.jar</systemPath>
</dependency>

View File

@ -43,13 +43,13 @@ class CIMCacheManager {
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
public static final String KEY_NTC_SWITCH = "KEY_NTC_SWITCH";
public static final String KEY_NTC_CHANNEL_NAME = "KEY_NTC_CHANNEL_NAME";
public static final String KEY_NTC_CHANNEL_MESSAGE = "KEY_NTC_CHANNEL_MESSAGE";
public static final String KEY_NTC_CHANNEL_ICON = "KEY_NTC_CHANNEL_ICON";
public static final String KEY_NTC_CHANNEL_ICON = "KEY_NTC_ICON";
public static final String CONTENT_URI = "content://%s.cim.provider";

View File

@ -152,7 +152,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
private void connect(long delay) {
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.putExtra(BundleKey.KEY_DELAYED_TIME, delay);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION);
CIMPushManager.startService(context, serviceIntent);
}

View File

@ -67,7 +67,7 @@ public class CIMPushManager {
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION);
startService(context, serviceIntent);
}
@ -172,7 +172,7 @@ public class CIMPushManager {
}
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ServiceAction.ACTION_CIM_CONNECTION_PONG);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION);
startService(context, serviceIntent);
}
@ -239,7 +239,7 @@ public class CIMPushManager {
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, true);
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ServiceAction.ACTION_CLOSE_CIM_CONNECTION);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION);
startService(context, serviceIntent);
}
@ -253,7 +253,7 @@ public class CIMPushManager {
CIMCacheManager.remove(context, CIMCacheManager.KEY_UID);
Intent serviceIntent = new Intent(context, CIMPushService.class);
serviceIntent.setAction(ServiceAction.ACTION_DESTROY_CIM_SERVICE);
serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION);
startService(context, serviceIntent);
}

View File

@ -41,6 +41,7 @@ import com.farsunset.cim.sdk.android.model.Pong;
import com.farsunset.cim.sdk.android.model.SentBody;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
/**
* 与服务端连接服务
@ -49,9 +50,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/
public class CIMPushService extends Service {
private static final String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID";
private static final String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID";
private static final String TRANSIENT_NTC_CHANNEL_ID = "PUSH_TRANSIENT_NTC_ID";
private static final String PERSIST_NTC_CHANNEL_ID = "PUSH_PERSIST_NTC_ID";
private static final int NOTIFICATION_ID = Integer.MAX_VALUE;
private CIMConnectManager connectManager;
@ -59,7 +59,6 @@ public class CIMPushService extends Service {
private KeepAliveBroadcastReceiver keepAliveReceiver;
private ConnectivityManager connectivityManager;
private NotificationManager notificationManager;
private final AtomicBoolean persistHolder = new AtomicBoolean(false);
@Override
public void onCreate() {
@ -68,30 +67,26 @@ public class CIMPushService extends Service {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
keepAliveReceiver = new KeepAliveBroadcastReceiver();
registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter());
}
keepAliveReceiver = new KeepAliveBroadcastReceiver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter());
connectivityManager = getSystemService(ConnectivityManager.class);
connectivityManager = getSystemService(ConnectivityManager.class);
connectivityManager.registerDefaultNetworkCallback(networkCallback);
connectivityManager.registerDefaultNetworkCallback(networkCallback);
}
}
private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED));
sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED).setPackage(getPackageName()));
handleKeepAlive();
}
@Override
public void onLost(Network network) {
sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED));
sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED).setPackage(getPackageName()));
}
};
@ -105,16 +100,15 @@ public class CIMPushService extends Service {
private final Handler notificationHandler = new Handler() {
@Override
public void handleMessage(android.os.Message message) {
if (persistHolder.get()){
return;
if (!CIMCacheManager.getBoolean(CIMPushService.this,CIMCacheManager.KEY_NTC_SWITCH)){
stopForeground(true);
}
stopForeground(true);
}
};
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return super.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter(),Context.RECEIVER_EXPORTED);
}else {
return super.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter());
@ -136,9 +130,9 @@ public class CIMPushService extends Service {
String action = newIntent.getAction();
createTransientNotification(newIntent);
createTransientNotification();
if (ServiceAction.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
if (ServiceAction.ACTION_CREATE_CONNECTION.equals(action)) {
this.prepareConnect(newIntent.getLongExtra(BundleKey.KEY_DELAYED_TIME, 0));
}
@ -146,7 +140,7 @@ public class CIMPushService extends Service {
connectManager.send((SentBody) newIntent.getSerializableExtra(BundleKey.KEY_SEND_BODY));
}
if (ServiceAction.ACTION_CLOSE_CIM_CONNECTION.equals(action)) {
if (ServiceAction.ACTION_CLOSE_CONNECTION.equals(action)) {
connectManager.close();
}
@ -154,12 +148,12 @@ public class CIMPushService extends Service {
handleKeepAlive();
}
if (ServiceAction.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
if (ServiceAction.ACTION_DESTROY_SERVICE.equals(action)) {
connectManager.close();
this.stopSelf();
}
if (ServiceAction.ACTION_CIM_CONNECTION_PONG.equals(action)) {
if (ServiceAction.ACTION_CONNECTION_PONG.equals(action)) {
connectManager.send(Pong.getInstance());
}
@ -168,21 +162,20 @@ public class CIMPushService extends Service {
CIMLogger.getLogger().debugMode(enable);
}
if (ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) {
this.stopForeground(true);
CIMCacheManager.putBoolean(this, CIMCacheManager.KEY_NTC_SWITCH,false);
}
if (ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) {
createPersistNotification(newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_CHANNEL),
newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_MESSAGE),
newIntent.getIntExtra(BundleKey.KEY_NOTIFICATION_ICON,0));
persistHolder.set(true);
newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_MESSAGE),
newIntent.getIntExtra(BundleKey.KEY_NOTIFICATION_ICON,0));
return super.onStartCommand(intent,flags,startId);
}
if (ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) {
stopForeground(true);
persistHolder.set(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationHandler.sendEmptyMessageDelayed(0, 200);
}
notificationHandler.sendEmptyMessageDelayed(0, 200);
return super.onStartCommand(intent,flags,startId);
}
@ -208,7 +201,7 @@ public class CIMPushService extends Service {
String host = CIMCacheManager.getString(this, CIMCacheManager.KEY_CIM_SERVER_HOST);
int port = CIMCacheManager.getInt(this, CIMCacheManager.KEY_CIM_SERVER_PORT);
if (host == null || host.trim().length() == 0 || port <= 0) {
if (host == null || host.trim().isEmpty() || port <= 0) {
Log.e(this.getClass().getSimpleName(), "Invalid hostname or port. host:" + host + " port:" + port);
return;
}
@ -242,39 +235,18 @@ public class CIMPushService extends Service {
stopForeground(true);
persistHolder.set(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
unregisterReceiver(keepAliveReceiver);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.unregisterNetworkCallback(networkCallback);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) != null) {
notificationManager.deleteNotificationChannel(TRANSIENT_NTC_CHANNEL_ID);
}
unregisterReceiver(keepAliveReceiver);
connectivityManager.unregisterNetworkCallback(networkCallback);
}
private void createTransientNotification(Intent intent) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || persistHolder.get()) {
return;
}
if (ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION.equals(intent.getAction())
|| ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(intent.getAction())) {
return;
}
private void createTransientNotification() {
if (notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) != null) {
int icon = CIMCacheManager.getInt(this,CIMCacheManager.KEY_NTC_CHANNEL_ICON);
String title = CIMCacheManager.getString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME);
String message = CIMCacheManager.getString(this,CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE);
int icon = CIMCacheManager.getInt(this, CIMCacheManager.KEY_NTC_CHANNEL_ICON);
String title = CIMCacheManager.getString(this, CIMCacheManager.KEY_NTC_CHANNEL_NAME);
String message = CIMCacheManager.getString(this, CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE);
Notification notification = makeNotification(PERSIST_NTC_CHANNEL_ID,icon,title,message);
@ -291,7 +263,7 @@ public class CIMPushService extends Service {
notificationManager.createNotificationChannel(channel);
}
Notification notification = makeNotification(TRANSIENT_NTC_CHANNEL_ID,0,CIMPushService.class.getSimpleName(),null);
Notification notification = makeNotification(TRANSIENT_NTC_CHANNEL_ID,0, CIMPushService.class.getSimpleName(),null);
startForegroundNotification(NOTIFICATION_ID, notification);
@ -300,11 +272,12 @@ public class CIMPushService extends Service {
private void createPersistNotification(String channelName ,String message,int icon) {
CIMCacheManager.putString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME,channelName);
CIMCacheManager.putString(this,CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE,message);
CIMCacheManager.putInt(this,CIMCacheManager.KEY_NTC_CHANNEL_ICON,icon);
CIMCacheManager.putString(this, CIMCacheManager.KEY_NTC_CHANNEL_NAME,channelName);
CIMCacheManager.putString(this, CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE,message);
CIMCacheManager.putInt(this, CIMCacheManager.KEY_NTC_CHANNEL_ICON,icon);
CIMCacheManager.putBoolean(this, CIMCacheManager.KEY_NTC_SWITCH,true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) {
if (notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(PERSIST_NTC_CHANNEL_ID,channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false);
channel.setShowBadge(false);
@ -322,18 +295,14 @@ public class CIMPushService extends Service {
private Notification makeNotification(String channel,int icon,String title,String message){
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
builder = new Notification.Builder(this,channel);
}else {
builder = new Notification.Builder(this);
}
builder = new Notification.Builder(this, channel);
builder.setAutoCancel(false)
.setOngoing(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(getPendingIntent())
.setContentTitle(title)
.setContentText(message);
.setOngoing(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(getPendingIntent())
.setContentTitle(title)
.setContentText(message);
if (icon > 0){
builder.setSmallIcon(icon);

View File

@ -23,15 +23,15 @@ package com.farsunset.cim.sdk.android.constant;
public interface ServiceAction {
String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
String ACTION_CREATE_CONNECTION = "ACTION_CREATE_CONNECTION";
String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE";
String ACTION_DESTROY_SERVICE = "ACTION_DESTROY_SERVICE";
String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
String ACTION_CLOSE_CONNECTION = "ACTION_CLOSE_CONNECTION";
String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE";
@ -39,6 +39,5 @@ public interface ServiceAction {
String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION";
String ACTION_CIM_CONNECTION_PONG = "ACTION_CIM_CONNECTION_PONG";
String ACTION_CONNECTION_PONG = "ACTION_CONNECTION_PONG";
}

View File

@ -1,22 +1,26 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 33
buildToolsVersion '33.0.0'
namespace 'com.farsunset.cim'
defaultConfig {
applicationId "com.farsunset.cim"
minSdkVersion 23
targetSdkVersion 33
targetSdkVersion 35
compileSdk 35
versionCode 1
versionName "1.0"
/*
CIM服务器,IP修改为自己的服务器IP
*/
buildConfigField("String", "CIM_API_URL", '"http://192.168.31.175:8080"')
buildConfigField("String", "CIM_SERVER_HOST", '"192.168.31.175"')
buildConfigField("Integer", "CIM_SERVER_PORT", '23456')
buildConfigField("String", "CIM_API_URL", '"http://114.215.199.17:8080"')
buildConfigField("String", "CIM_SERVER_HOST", '"114.215.199.17"')
buildConfigField("Integer", "CIM_SERVER_PORT", '34567')
}
buildFeatures {
buildConfig = true
}
buildTypes {
@ -32,12 +36,13 @@ android {
}
dependencies {
implementation "com.farsunset:cim-android-sdk:4.2.13"
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.protobuf:protobuf-javalite:3.22.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'androidx.appcompat:appcompat:1.7.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.annotation:annotation:1.9.1'
implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
implementation 'com.google.protobuf:protobuf-javalite:3.25.4'
implementation 'com.squareup.retrofit2:retrofit:3.0.0'
implementation 'com.squareup.retrofit2:converter-gson:3.0.0'
implementation "com.farsunset:cim-android-sdk:4.2.15"
}

View File

@ -2,6 +2,7 @@
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">true</item>
<item name="colorPrimary">#FFFFFF</item>
<item name="colorAccent">#0CB069</item>

View File

@ -1,27 +1,3 @@
buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven{ url'https://maven.aliyun.com/repository/central' }
maven{ url'https://maven.aliyun.com/repository/google' }
maven{ url'https://maven.aliyun.com/repository/public' }
maven{ url'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.0'
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven{ url'https://maven.aliyun.com/repository/central' }
maven{ url'https://maven.aliyun.com/repository/google' }
maven{ url'https://maven.aliyun.com/repository/public' }
maven{ url'https://maven.aliyun.com/repository/gradle-plugin' }
}
plugins {
id 'com.android.application' version '8.9.1' apply false
}

View File

@ -1,6 +1,6 @@
#Wed Feb 15 10:18:48 CST 2023
#Mon Jun 09 11:38:39 CST 2025
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,2 +1,27 @@
pluginManagement {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
maven{ url'https://maven.aliyun.com/repository/central' }
maven{ url'https://maven.aliyun.com/repository/google' }
maven{ url'https://maven.aliyun.com/repository/public' }
maven{ url'https://maven.aliyun.com/repository/gradle-plugin' }
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
maven{ url'https://maven.aliyun.com/repository/central' }
maven{ url'https://maven.aliyun.com/repository/google' }
maven{ url'https://maven.aliyun.com/repository/public' }
maven{ url'https://maven.aliyun.com/repository/gradle-plugin' }
}
}
include ':app'
rootProject.name = "cim-client-android"