Skip to content

Commit 709d7fa

Browse files
author
Jack Jiang
committed
Andriod版Demo增加前台服务,提升保活能力
1 parent ac2da05 commit 709d7fa

File tree

14 files changed

+434
-16
lines changed

14 files changed

+434
-16
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

demo_src/TCP_Client/MobileIMSDK4aDemo_tcp/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
minSdkVersion 14
88
targetSdkVersion 29
99
versionCode 104
10-
versionName "v5.0b200917.1"
10+
versionName "v5.0b201015.3"
1111
//testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

demo_src/TCP_Client/MobileIMSDK4aDemo_tcp/app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
77
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
8+
<!-- Android 9.0中需要此权限的添加,不然崩溃 -->
9+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
810

911
<application
1012
android:allowBackup="true"
@@ -13,7 +15,7 @@
1315
android:theme="@style/AppTheme"
1416
tools:ignore="GoogleAppIndexingWarning">
1517
<activity
16-
android:name="net.x52im.mobileimsdk.android.demo.SplashScreenActivity"
18+
android:name=".SplashScreenActivity"
1719
android:label="@string/app_name"
1820
android:screenOrientation="portrait"
1921
android:theme="@style/AppThemeBlackBar"
@@ -25,21 +27,24 @@
2527
</intent-filter>
2628
</activity>
2729
<activity
28-
android:name="net.x52im.mobileimsdk.android.demo.LoginActivity"
30+
android:name=".LoginActivity"
2931
android:configChanges="screenSize|keyboardHidden|orientation"
3032
android:screenOrientation="portrait"
3133
tools:ignore="LockedOrientationActivity"
3234
android:theme="@style/AppTheme"
3335
android:windowSoftInputMode="adjustUnspecified|stateHidden" >
3436
</activity>
3537
<activity
36-
android:name="net.x52im.mobileimsdk.android.demo.MainActivity"
38+
android:name=".MainActivity"
3739
android:configChanges="screenSize|keyboardHidden|orientation"
3840
android:screenOrientation="portrait"
3941
tools:ignore="LockedOrientationActivity"
4042
android:theme="@style/AppTheme"
4143
android:windowSoftInputMode="adjustUnspecified|stateHidden">
4244
</activity>
43-
</application>
4445

46+
<!-- 一个为了Demo能更好保活的前台服务 -->
47+
<service android:name=".service.GeniusService" />
48+
49+
</application>
4550
</manifest>

demo_src/TCP_Client/MobileIMSDK4aDemo_tcp/app/src/main/java/net/x52im/mobileimsdk/android/demo/LoginActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import net.x52im.mobileimsdk.android.conf.ConfigEntity;
2323
import net.x52im.mobileimsdk.android.core.LocalDataSender;
2424
import net.x52im.mobileimsdk.android.core.LocalSocketProvider;
25-
import net.x52im.mobileimsdk.android.demo.R;
2625

2726
import android.app.Activity;
2827
import android.app.AlertDialog;

demo_src/TCP_Client/MobileIMSDK4aDemo_tcp/app/src/main/java/net/x52im/mobileimsdk/android/demo/MainActivity.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525

2626
import net.x52im.mobileimsdk.android.ClientCoreSDK;
2727
import net.x52im.mobileimsdk.android.core.LocalDataSender;
28-
import net.x52im.mobileimsdk.android.demo.R;
28+
import net.x52im.mobileimsdk.android.demo.service.GeniusService;
2929

30+
import android.content.ComponentName;
3031
import android.content.Context;
32+
import android.content.Intent;
33+
import android.content.ServiceConnection;
3134
import android.graphics.Color;
3235
import android.os.AsyncTask;
3336
import android.os.Bundle;
37+
import android.os.IBinder;
3438
import android.util.Log;
3539
import android.view.LayoutInflater;
3640
import android.view.View;
@@ -77,6 +81,10 @@ protected void onCreate(Bundle savedInstanceState)
7781
initViews();
7882
initListeners();
7983
initOthers();
84+
85+
// 启动前台服务(注意:该服务仅用于提升Demo的运行优先级,确保在高版本
86+
// Andriod系统上进程保活和网络保活,此服务与SDK本身无关,也不是必须的)
87+
doBindService();
8088
}
8189

8290
/**
@@ -87,19 +95,23 @@ public void onBackPressed()
8795
{
8896
super.onBackPressed();
8997

90-
// ** 注意:Android程序要么就别处理,要处理就一定
91-
// 要退干净,否则会有意想不到的问题哦!
98+
// ** 注意:Android程序要么就别处理,要处理就一定要退干净,否则会有意想不到的问题哦!
9299
// 退出登陆
93100
doLogout();
94101
// 退出程序
95102
doExit();
96103
}
97-
104+
105+
@Override
98106
protected void onDestroy()
99107
{
100108
// 释放IM占用资源
101109
IMClientManager.getInstance(this).release();
102-
//
110+
111+
// 解绑前台服务(注意:该服务仅用于提升Demo的运行优先级,确保在高版本
112+
// Andriod系统上进程保活和网络保活,此服务与SDK本身无关,也不是必须的)
113+
doUnbindService();
114+
103115
super.onDestroy();
104116
}
105117

@@ -357,4 +369,44 @@ public enum ChatInfoColorType
357369
green,
358370
}
359371
//--------------------------------------------------------------- inner classes END
372+
373+
//--------------------------------------------------------------- 前台服务相关代码 START
374+
/** 前台服务对象(绑定MobileIMSDK的Demo后,确保Demo能常驻内存,因为Andriod高版本对于进程保活、网络保活现在限制非常严格) */
375+
private GeniusService boundService;
376+
377+
/** 绑定时需要使用的连接对象 */
378+
private ServiceConnection serviceConnection = new ServiceConnection()
379+
{
380+
public void onServiceConnected(ComponentName className, IBinder service)
381+
{
382+
boundService = ((GeniusService.LocalBinder)service).getService();
383+
}
384+
385+
public void onServiceDisconnected(ComponentName className)
386+
{
387+
boundService = null;
388+
}
389+
};
390+
391+
/**
392+
* 将本activity与后台服务绑定起来.
393+
*/
394+
protected void doBindService()
395+
{
396+
this.getApplicationContext().bindService(new Intent(this.getApplicationContext(), GeniusService.class), serviceConnection, Context.BIND_AUTO_CREATE);
397+
}
398+
399+
/**
400+
* 解绑服务(服务将失去功能,随时会被系统回收).
401+
*/
402+
protected void doUnbindService()
403+
{
404+
try{
405+
this.getApplicationContext().unbindService(serviceConnection);
406+
}
407+
catch (Exception e){
408+
// Log.w(TAG, e);
409+
}
410+
}
411+
//--------------------------------------------------------------- 前台服务相关代码 END
360412
}

demo_src/TCP_Client/MobileIMSDK4aDemo_tcp/app/src/main/java/net/x52im/mobileimsdk/android/demo/SplashScreenActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import android.view.animation.Animation;
2929
import android.view.animation.Animation.AnimationListener;
3030

31-
import net.x52im.mobileimsdk.android.demo.R;
32-
3331
/**
3432
* 应用程序启动类:显示闪屏界面并跳转到主界面.
3533
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package net.x52im.mobileimsdk.android.demo.service;
2+
3+
import android.app.Application;
4+
import android.app.Notification;
5+
import android.app.NotificationChannel;
6+
import android.app.NotificationManager;
7+
import android.app.PendingIntent;
8+
import android.app.Service;
9+
import android.content.Context;
10+
import android.content.Intent;
11+
import android.graphics.Color;
12+
import android.os.Binder;
13+
import android.os.IBinder;
14+
import android.util.Log;
15+
16+
import net.x52im.mobileimsdk.android.demo.R;
17+
import net.x52im.mobileimsdk.android.demo.SplashScreenActivity;
18+
19+
import androidx.core.app.NotificationCompat;
20+
21+
/**
22+
* 一个用于演示的前台服务实现类(本类代码,来自于RainbowChat产品:http://www.52im.net/thread-19-1-1.html)。
23+
* <p>
24+
* 目前的唯一作用是:作为前台服务,提升Demo的运行优先级,确保在高版本Andriod系统上进程保活和网络保活.
25+
*
26+
* 注意:该服务与MobileIMSDK本身无关,也不是必须的!
27+
*
28+
* @author Jack Jiang(http://www.52im.net/space-uid-1.html)
29+
* @version 1.0
30+
*/
31+
public class GeniusService extends Service
32+
{
33+
final static String TAG = GeniusService.class.getSimpleName();
34+
35+
private NotificationManager mNM;
36+
37+
/**
38+
* Class for clients to access. Because we know this service always runs in
39+
* the same process as its clients, we don't need to deal with IPC.
40+
*/
41+
public class LocalBinder extends Binder
42+
{
43+
public GeniusService getService()
44+
{
45+
return GeniusService.this;
46+
}
47+
}
48+
49+
@Override
50+
public void onCreate()
51+
{
52+
mNM = getNotificationManager(this);
53+
54+
// Display a notification about us starting. We put an icon in the status bar.
55+
showNotification();
56+
}
57+
58+
@Override
59+
public void onDestroy()
60+
{
61+
//将service从前台移除,并允许随时被系统回收
62+
this.stopForeground(true);
63+
// Tell the user we stopped.
64+
Log.d(TAG, "服务Destroy了哦!");
65+
}
66+
67+
@Override
68+
public IBinder onBind(Intent intent)
69+
{
70+
return mBinder;
71+
}
72+
73+
// This is the object that receives interactions from clients. See RemoteService for a more complete example.
74+
private final IBinder mBinder = new LocalBinder();
75+
76+
/**
77+
* Show a notification while this service is running.
78+
*/
79+
private void showNotification()
80+
{
81+
Application app = (Application)this.getApplicationContext();
82+
83+
Intent intent = new Intent(Intent.ACTION_MAIN);
84+
intent.addCategory(Intent.CATEGORY_LAUNCHER);
85+
intent.setClass(this, SplashScreenActivity.class);
86+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
87+
88+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
89+
90+
String appName = this.getResources().getString(R.string.app_name);
91+
92+
// 创建一个Notification
93+
Notification notification = createNotification(app , contentIntent, appName+" 正在运行中 ..."
94+
, "点击回到 "+appName+" 的Demo", R.drawable.icon);
95+
96+
// 让service在前台执行
97+
this.startForeground(999, notification);
98+
}
99+
100+
public static NotificationManager getNotificationManager(Context context)
101+
{
102+
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
103+
104+
// 以下代码,解决在Android 8及以上代码中,无法正常显示Notification或报"Bad notification for startForeground"等问题
105+
NotificationChannel notificationChannel = null;
106+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
107+
{
108+
notificationChannel = new NotificationChannel("default_1", "Default Channel", NotificationManager.IMPORTANCE_HIGH);
109+
notificationChannel.enableLights(true);
110+
111+
notificationChannel.setLightColor(Color.RED);
112+
notificationChannel.setShowBadge(true);
113+
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
114+
115+
manager.createNotificationChannel(notificationChannel);
116+
}
117+
118+
return manager;
119+
}
120+
121+
/**
122+
* 兼容地方法创建Notification方法。
123+
*
124+
* @param context
125+
* @param pendingIntent
126+
* @param title
127+
* @param text
128+
* @param iconId
129+
* @return
130+
*/
131+
public static Notification createNotification(Context context, PendingIntent pendingIntent, String title, String text, int iconId)
132+
{
133+
// 创建一个Notification Builder,使用NotificationCompat可以更好的兼容Android各系统版本,
134+
// 有关Android Notitication的兼容性、详细设置等,参见:https://www.cnblogs.com/travellife/p/Android-Notification-xiang-jie.html
135+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "default_1")
136+
.setContentTitle(title)
137+
.setContentText(text)
138+
.setContentIntent(pendingIntent)
139+
// 设置显示在手机最上边的状态栏的图标
140+
.setSmallIcon(iconId);
141+
142+
// 通知的显示等级(Android5.0开始,通知可以显示在锁屏上):
143+
// - VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容
144+
// - VISIBILITY_PUBLIC : 显示通知的全部内容
145+
// - VISIBILITY_SECRET : 不显示任何内容,包括图标
146+
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
147+
148+
// 创建一个Notification
149+
Notification notification = builder.build();
150+
151+
return notification;
152+
}
153+
}
Binary file not shown.

demo_src/UDP_Client/MobileIMSDK4aDemo_udp/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
minSdkVersion 14
88
targetSdkVersion 29
99
versionCode 105
10-
versionName "v5.0b200917.1"
10+
versionName "v5.0b201015.1"
1111
//testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

demo_src/UDP_Client/MobileIMSDK4aDemo_udp/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
77
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
8+
<!-- Android 9.0中需要此权限的添加,不然崩溃 -->
9+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
810

911
<application
1012
android:allowBackup="true"
@@ -39,6 +41,9 @@
3941
android:windowSoftInputMode="adjustUnspecified|stateHidden"
4042
tools:ignore="LockedOrientationActivity">
4143
</activity>
42-
</application>
4344

45+
<!-- 一个为了Demo能更好保活的前台服务 -->
46+
<service android:name=".service.GeniusService" />
47+
48+
</application>
4449
</manifest>

0 commit comments

Comments
 (0)