service
This commit is contained in:
parent
0f679ca19f
commit
f07336971b
@ -1,29 +1,35 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
|
||||||
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
|
|
||||||
tools:ignore="ProtectedPermissions"></uses-permission>
|
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
|
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
|
||||||
|
tools:ignore="ProtectedPermissions" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
|
||||||
tools:ignore="ScopedStorage" />
|
tools:ignore="ScopedStorage" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:requestLegacyExternalStorage="true"
|
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
android:extractNativeLibs="true"
|
||||||
android:fullBackupContent="@xml/backup_rules"
|
android:fullBackupContent="@xml/backup_rules"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
|
android:requestLegacyExternalStorage="true"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.Gost"
|
android:theme="@style/Theme.Gost"
|
||||||
android:extractNativeLibs="true"
|
|
||||||
tools:targetApi="31">
|
tools:targetApi="31">
|
||||||
|
|
||||||
|
<service
|
||||||
|
android:name=".GostService"
|
||||||
|
android:enabled="true" />
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
49
app/src/main/java/run/evan/gost/GostService.java
Normal file
49
app/src/main/java/run/evan/gost/GostService.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package run.evan.gost;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.os.Binder;
|
||||||
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
public class GostService extends Service {
|
||||||
|
|
||||||
|
private static final String CHANNEL_ID = "GostService";
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
|
NotificationChannel Channel = new NotificationChannel(CHANNEL_ID, "GOST", NotificationManager.IMPORTANCE_HIGH);
|
||||||
|
Channel.enableLights(false);
|
||||||
|
Channel.setLightColor(Color.RED);
|
||||||
|
Channel.setShowBadge(true);
|
||||||
|
Channel.setDescription( "GOST");
|
||||||
|
Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
|
||||||
|
manager.createNotificationChannel(Channel);
|
||||||
|
|
||||||
|
Notification notification = new Notification.Builder(this)
|
||||||
|
.setChannelId(CHANNEL_ID)
|
||||||
|
.setContentTitle("GOST")//标题
|
||||||
|
.setContentText(getResources().getString(R.string.service_text))//内容
|
||||||
|
.setWhen(System.currentTimeMillis())
|
||||||
|
.setSmallIcon(R.mipmap.ic_launcher)//小图标一定需要设置,否则会报错(如果不设置它启动服务前台化不会报错,但是你会发现这个通知不会启动),如果是普通通知,不设置必然报错
|
||||||
|
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
|
||||||
|
.build();
|
||||||
|
startForeground(1, notification);//服务前台化只能使用startForeground()方法,不能使用 notificationManager.notify(1,notification); 这个只是启动通知使用的,使用这个方法你只需要等待几秒就会发现报错了
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -61,6 +61,8 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
MyHandler handler = new MyHandler(this);
|
MyHandler handler = new MyHandler(this);
|
||||||
|
|
||||||
|
Intent gostServiceIntent;
|
||||||
|
|
||||||
private static final String TAG = "run.evan.gost";
|
private static final String TAG = "run.evan.gost";
|
||||||
|
|
||||||
|
|
||||||
@ -88,10 +90,12 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
case 2:
|
case 2:
|
||||||
activity.btnStart.setText(R.string.btn_stop);
|
activity.btnStart.setText(R.string.btn_stop);
|
||||||
activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-1499549));
|
activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-1499549));
|
||||||
|
activity.getApplicationContext().startForegroundService(activity.gostServiceIntent);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
activity.btnStart.setText(R.string.btn_start);
|
activity.btnStart.setText(R.string.btn_start);
|
||||||
activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-11751600));
|
activity.btnStart.setBackgroundTintList(ColorStateList.valueOf(-11751600));
|
||||||
|
activity.getApplicationContext().stopService(activity.gostServiceIntent);
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,6 +107,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
gostServiceIntent = new Intent(MainActivity.this, GostService.class);
|
||||||
appPreferences = getSharedPreferences("app", MODE_PRIVATE);
|
appPreferences = getSharedPreferences("app", MODE_PRIVATE);
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
||||||
@ -121,10 +126,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!isIgnoringBatteryOptimizations()) {
|
|
||||||
requestIgnoreBatteryOptimizations();
|
|
||||||
}
|
|
||||||
textView1 = findViewById(R.id.textView);
|
textView1 = findViewById(R.id.textView);
|
||||||
textView1.setMovementMethod(ScrollingMovementMethod.getInstance());
|
textView1.setMovementMethod(ScrollingMovementMethod.getInstance());
|
||||||
configEditText = findViewById(R.id.configEditText);
|
configEditText = findViewById(R.id.configEditText);
|
||||||
@ -323,27 +324,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
private void requestIgnoreBatteryOptimizations() {
|
|
||||||
try {
|
|
||||||
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
|
||||||
intent.setData(Uri.parse("package:" + getPackageName()));
|
|
||||||
startActivity(intent);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
private boolean isIgnoringBatteryOptimizations() {
|
|
||||||
boolean isIgnoring = false;
|
|
||||||
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
||||||
if (powerManager != null) {
|
|
||||||
isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
|
|
||||||
}
|
|
||||||
return isIgnoring;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 带回授权结果
|
// 带回授权结果
|
||||||
@Override
|
@Override
|
||||||
|
@ -15,4 +15,5 @@
|
|||||||
<string name="title_yes">是</string>
|
<string name="title_yes">是</string>
|
||||||
<string name="title_no">否</string>
|
<string name="title_no">否</string>
|
||||||
<string name="tip_config_not_selected">未选择配置</string>
|
<string name="tip_config_not_selected">未选择配置</string>
|
||||||
|
<string name="service_text">GOST运行中</string>
|
||||||
</resources>
|
</resources>
|
@ -14,4 +14,5 @@
|
|||||||
<string name="title_yes">Yes</string>
|
<string name="title_yes">Yes</string>
|
||||||
<string name="title_no">No</string>
|
<string name="title_no">No</string>
|
||||||
<string name="tip_config_not_selected">No config selected</string>
|
<string name="tip_config_not_selected">No config selected</string>
|
||||||
|
<string name="service_text">GOST is running</string>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in New Issue
Block a user