开机直接启动服务而不是界面

master
AceDroidX 1 year ago
parent 010860ddcd
commit e4b404a2aa

@ -5,7 +5,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

@ -3,6 +3,7 @@ package io.github.acedroidx.frp
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
class AutoStartBroadReceiver : BroadcastReceiver() {
@ -13,11 +14,13 @@ class AutoStartBroadReceiver : BroadcastReceiver() {
val auto_start = editor.getBoolean("auto_start", false)
if (ACTION == intent.action && auto_start) {
//开机启动
val mainIntent = Intent(context, MainActivity::class.java)
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(mainIntent)
val mainIntent = Intent(context, ShellService::class.java)
mainIntent.putExtra("filename", BuildConfig.FrpcFileName)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(mainIntent)
}else{
context.startService(mainIntent)
}
}
}
}

@ -66,7 +66,6 @@ class MainActivity : AppCompatActivity() {
val editor = getSharedPreferences("data", AppCompatActivity.MODE_PRIVATE)
auto_start_switch = findViewById<SwitchCompat>(R.id.auto_start_switch)
auto_start_switch.isChecked = editor.getBoolean("auto_start", false)
if (auto_start_switch.isChecked) (startShell())
auto_start_switch.setOnCheckedChangeListener { buttonView, isChecked ->
val editor = editor.edit()
editor.putBoolean("auto_start", isChecked)

@ -70,7 +70,7 @@ class ShellService : Service() {
stopSelf()
return START_NOT_STICKY
}
Toast.makeText(this, "已启动服务", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "已启动frp服务", Toast.LENGTH_SHORT).show()
startForeground(1, showMotification());
return START_NOT_STICKY
}
@ -78,7 +78,7 @@ class ShellService : Service() {
override fun onDestroy() {
p?.destroy()
p = null
Toast.makeText(this, "已关闭服务", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "已关闭frp服务", Toast.LENGTH_SHORT).show()
}
private fun showMotification(): Notification {

Loading…
Cancel
Save