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

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.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <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.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <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.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
class AutoStartBroadReceiver : BroadcastReceiver() { class AutoStartBroadReceiver : BroadcastReceiver() {
@ -13,11 +14,13 @@ class AutoStartBroadReceiver : BroadcastReceiver() {
val auto_start = editor.getBoolean("auto_start", false) val auto_start = editor.getBoolean("auto_start", false)
if (ACTION == intent.action && auto_start) { if (ACTION == intent.action && auto_start) {
//开机启动 //开机启动
val mainIntent = Intent(context, MainActivity::class.java) val mainIntent = Intent(context, ShellService::class.java)
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK mainIntent.putExtra("filename", BuildConfig.FrpcFileName)
context.startActivity(mainIntent) 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) val editor = getSharedPreferences("data", AppCompatActivity.MODE_PRIVATE)
auto_start_switch = findViewById<SwitchCompat>(R.id.auto_start_switch) auto_start_switch = findViewById<SwitchCompat>(R.id.auto_start_switch)
auto_start_switch.isChecked = editor.getBoolean("auto_start", false) auto_start_switch.isChecked = editor.getBoolean("auto_start", false)
if (auto_start_switch.isChecked) (startShell())
auto_start_switch.setOnCheckedChangeListener { buttonView, isChecked -> auto_start_switch.setOnCheckedChangeListener { buttonView, isChecked ->
val editor = editor.edit() val editor = editor.edit()
editor.putBoolean("auto_start", isChecked) editor.putBoolean("auto_start", isChecked)

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

Loading…
Cancel
Save