增加开机自启动 (#8)

This commit is contained in:
z156854666 2024-04-01 13:27:01 +08:00 committed by GitHub
parent db9d92c976
commit 8498c391b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 3 deletions

View File

@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<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.SYSTEM_ALERT_WINDOW" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -35,6 +37,18 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<receiver
android:name=".AutoStartBroadReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>

View File

@ -0,0 +1,23 @@
package io.github.acedroidx.frp
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
class AutoStartBroadReceiver : BroadcastReceiver() {
private val ACTION = "android.intent.action.BOOT_COMPLETED"
override fun onReceive(context: Context, intent: Intent) {
//开机启动
val editor = context.getSharedPreferences("data", AppCompatActivity.MODE_PRIVATE)
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)
}
}
}

View File

@ -1,6 +1,8 @@
package io.github.acedroidx.frp package io.github.acedroidx.frp
import android.app.* import android.app.ActivityManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -23,6 +25,7 @@ class MainActivity : AppCompatActivity() {
val configname = "config.ini" val configname = "config.ini"
private lateinit var state_switch: SwitchCompat private lateinit var state_switch: SwitchCompat
private lateinit var auto_start_switch: SwitchCompat
private lateinit var mService: ShellService private lateinit var mService: ShellService
private var mBound: Boolean = false private var mBound: Boolean = false
@ -60,6 +63,15 @@ class MainActivity : AppCompatActivity() {
state_switch = findViewById<SwitchCompat>(R.id.state_switch) state_switch = findViewById<SwitchCompat>(R.id.state_switch)
state_switch.isChecked = mBound state_switch.isChecked = mBound
state_switch.setOnCheckedChangeListener { buttonView, isChecked -> if (isChecked) (startShell()) else (stopShell()) } state_switch.setOnCheckedChangeListener { buttonView, isChecked -> if (isChecked) (startShell()) else (stopShell()) }
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)
editor.apply();
}
if (mBound) { if (mBound) {
val intent = Intent(this, ShellService::class.java) val intent = Intent(this, ShellService::class.java)
bindService(intent, connection, Context.BIND_AUTO_CREATE) bindService(intent, connection, Context.BIND_AUTO_CREATE)
@ -151,8 +163,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun isServiceRunning(serviceClass: Class<*>): Boolean { private fun isServiceRunning(serviceClass: Class<*>): Boolean {
val manager = val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) { for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) { if (serviceClass.name == service.service.className) {
return true return true

View File

@ -30,6 +30,12 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="切换开关" /> android:text="切换开关" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/auto_start_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开机自启动" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -46,6 +52,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="关于" /> android:text="关于" />
</LinearLayout> </LinearLayout>
<Space <Space

BIN
image/image3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
image/image4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB