Skip to content

Commit

Permalink
1. 适配高版本Android服务
Browse files Browse the repository at this point in the history
2. 增加后台运行,现在切换到后台Quicker连接不会立刻断开
3. 增加前台通知
  • Loading branch information
YSun-Top committed Oct 17, 2023
1 parent d96028d commit 3137047
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 187 deletions.
10 changes: 7 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Expand All @@ -15,6 +16,9 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--前台服务权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>

<application
android:name=".QuickerApplication"
Expand Down Expand Up @@ -99,8 +103,8 @@

<service
android:name=".client.ClientService"
android:enabled="true"
android:exported="true" />
android:foregroundServiceType="dataSync"
android:enabled="true" />
<service
android:name=".service.TaskManagerService"
android:enabled="true" />
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/cuiliang/quicker/QuickerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.util.DisplayMetrics;

import cuiliang.quicker.client.ClientService;
Expand All @@ -16,7 +17,11 @@ public void onCreate() {
super.onCreate();
displayMetrics = getResources().getDisplayMetrics();
SPUtils.init(this);
startService(new Intent(this, ClientService.class));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, ClientService.class));
} else {
startService(new Intent(this, ClientService.class));
}
startService(new Intent(this, TaskManagerService.class));

// closeHideApiDialog();
Expand Down
37 changes: 36 additions & 1 deletion app/src/main/java/cuiliang/quicker/client/ClientService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package cuiliang.quicker.client;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
Expand All @@ -20,6 +27,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cuiliang.quicker.MainActivity;
import cuiliang.quicker.R;
import cuiliang.quicker.events.ServerMessageEvent;
import cuiliang.quicker.events.WifiStatusChangeEvent;
import cuiliang.quicker.messages.MessageBase;
Expand Down Expand Up @@ -81,7 +90,7 @@ public ClientService() {
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();

createNotification();
//
// wifi 监控
IntentFilter filter = new IntentFilter();
Expand Down Expand Up @@ -210,4 +219,30 @@ public void connectCallback(boolean isSuccess, @Nullable Object obj) {
}
}
}

private void createNotification(){
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notification; //创建服务对象
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel=new NotificationChannel(getPackageName(),"Quicker", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(this,getPackageName());
}else {
notification = new NotificationCompat.Builder(this);
}
Notification notification1 =notification.setContentTitle("Quicker")
.setContentText("Quicker 连接服务")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification1);//这个就是之前说的startForeground
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import cuiliang.quicker.util.KLog
import cuiliang.quicker.util.ToastUtils

Expand All @@ -14,12 +16,16 @@ import cuiliang.quicker.util.ToastUtils
* @see MsgRequestData
*/
object ServerRequestFactory {
private val mHandler=Handler(Looper.getMainLooper())
fun decodeRequest(ctx: Context, data: MsgRequestData) {
when (data.extData) {
"ShareAndroid" -> {
when (data.operation) {
"copy" -> (ctx.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).let {
it.setPrimaryClip(ClipData.newPlainText(data.data, data.data))
"copy" -> {
(ctx.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(ClipData.newPlainText(data.data, data.data))
mHandler.post {
ToastUtils.showShort(ctx,"剪贴板增加: ${data.data}")
}
}

"open" -> if (data.data.startsWith("http:") || data.data.startsWith("https:")) {
Expand All @@ -31,6 +37,9 @@ object ServerRequestFactory {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
)
mHandler.post {
ToastUtils.showShort(ctx,"打开网页:${data.data}")
}
KLog.d("decodeRequest", "打开网页:${data.data}")
}
}
Expand Down
Loading

0 comments on commit 3137047

Please sign in to comment.