mirror of
https://github.com/pppscn/SmsForwarder
synced 2024-11-17 21:25:31 +00:00
优化:保活措施-播放无声音乐让后台一直运行(可能比较耗电,按需启用)
This commit is contained in:
parent
673bac5854
commit
1200264e4e
@ -156,6 +156,11 @@
|
||||
<service
|
||||
android:name=".service.BatteryService"
|
||||
android:enabled="true" />
|
||||
<service
|
||||
android:name=".service.MusicService"
|
||||
android:enabled="true"
|
||||
android:exported="true"
|
||||
tools:ignore="ExportedService" />
|
||||
<service
|
||||
android:name=".service.NotifyService"
|
||||
android:enabled="true"
|
||||
|
@ -39,6 +39,7 @@ import com.idormy.sms.forwarder.sender.SenderUtil;
|
||||
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
|
||||
import com.idormy.sms.forwarder.service.BatteryService;
|
||||
import com.idormy.sms.forwarder.service.FrontService;
|
||||
import com.idormy.sms.forwarder.service.MusicService;
|
||||
import com.idormy.sms.forwarder.utils.CommonUtil;
|
||||
import com.idormy.sms.forwarder.utils.HttpUtil;
|
||||
import com.idormy.sms.forwarder.utils.KeepAliveUtils;
|
||||
@ -106,6 +107,16 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
|
||||
Log.e(TAG, "BatteryService:", e);
|
||||
}
|
||||
|
||||
//后台播放无声音乐
|
||||
if (SettingUtil.getPlaySilenceMusic()) {
|
||||
try {
|
||||
Intent musicServiceIntent = new Intent(this, MusicService.class);
|
||||
musicServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startService(musicServiceIntent);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MusicService:", e);
|
||||
}
|
||||
}
|
||||
|
||||
HttpUtil.init(this);
|
||||
//启用HttpServer
|
||||
|
@ -17,6 +17,7 @@ import com.idormy.sms.forwarder.receiver.SimStateReceiver;
|
||||
import com.idormy.sms.forwarder.sender.SendHistory;
|
||||
import com.idormy.sms.forwarder.service.BatteryService;
|
||||
import com.idormy.sms.forwarder.service.FrontService;
|
||||
import com.idormy.sms.forwarder.service.MusicService;
|
||||
import com.idormy.sms.forwarder.utils.Define;
|
||||
import com.idormy.sms.forwarder.utils.PermissionInterceptor;
|
||||
import com.idormy.sms.forwarder.utils.PhoneUtils;
|
||||
@ -108,6 +109,11 @@ public class MyApplication extends Application {
|
||||
Intent batteryServiceIntent = new Intent(this, BatteryService.class);
|
||||
startService(batteryServiceIntent);
|
||||
|
||||
//后台播放无声音乐
|
||||
if (SettingUtil.getPlaySilenceMusic()) {
|
||||
startService(new Intent(context, MusicService.class));
|
||||
}
|
||||
|
||||
//SIM卡插拔状态广播监听
|
||||
IntentFilter simStateFilter = new IntentFilter(SimStateReceiver.ACTION_SIM_STATE_CHANGED);
|
||||
registerReceiver(new SimStateReceiver(), simStateFilter);
|
||||
|
@ -24,6 +24,7 @@ import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
@ -37,6 +38,7 @@ import com.idormy.sms.forwarder.receiver.RebootBroadcastReceiver;
|
||||
import com.idormy.sms.forwarder.sender.HttpServer;
|
||||
import com.idormy.sms.forwarder.sender.SenderUtil;
|
||||
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
|
||||
import com.idormy.sms.forwarder.service.MusicService;
|
||||
import com.idormy.sms.forwarder.utils.CommonUtil;
|
||||
import com.idormy.sms.forwarder.utils.DbHelper;
|
||||
import com.idormy.sms.forwarder.utils.Define;
|
||||
@ -100,11 +102,13 @@ public class SettingActivity extends AppCompatActivity {
|
||||
editBatteryLevelAlarm(findViewById(R.id.et_battery_level_alarm_min), findViewById(R.id.et_battery_level_alarm_max), findViewById(R.id.cb_battery_level_alarm_once));
|
||||
|
||||
//开机启动
|
||||
checkWithReboot(findViewById(R.id.switch_with_reboot));
|
||||
checkWithReboot(findViewById(R.id.switch_with_reboot), findViewById(R.id.tv_auto_startup));
|
||||
//电池优化设置
|
||||
batterySetting(findViewById(R.id.layout_battery_setting), findViewById(R.id.switch_battery_setting));
|
||||
//不在最近任务列表中显示
|
||||
switchExcludeFromRecents(findViewById(R.id.switch_exclude_from_recents));
|
||||
//后台播放无声音乐
|
||||
switchPlaySilenceMusic(findViewById(R.id.switch_play_silence_music));
|
||||
//接口请求失败重试时间间隔
|
||||
editRetryDelayTime(findViewById(R.id.et_retry_times), findViewById(R.id.et_delay_time));
|
||||
|
||||
@ -426,7 +430,9 @@ public class SettingActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
//开机启动
|
||||
private void checkWithReboot(@SuppressLint("UseSwitchCompatOrMaterialCode") Switch withrebootSwitch) {
|
||||
private void checkWithReboot(@SuppressLint("UseSwitchCompatOrMaterialCode") Switch withrebootSwitch, TextView tvAutoStartup) {
|
||||
tvAutoStartup.setText(getAutoStartTips());
|
||||
|
||||
//获取组件
|
||||
final ComponentName cm = new ComponentName(this.getPackageName(), RebootBroadcastReceiver.class.getName());
|
||||
|
||||
@ -665,6 +671,23 @@ public class SettingActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
//后台播放无声音乐
|
||||
@SuppressLint("ObsoleteSdkInt,UseSwitchCompatOrMaterialCode")
|
||||
private void switchPlaySilenceMusic(Switch switch_play_silence_music) {
|
||||
switch_play_silence_music.setChecked(SettingUtil.getPlaySilenceMusic());
|
||||
|
||||
switch_play_silence_music.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
SettingUtil.switchPlaySilenceMusic(isChecked);
|
||||
|
||||
if (isChecked) {
|
||||
startService(new Intent(context, MusicService.class));
|
||||
} else {
|
||||
stopService(new Intent(context, MusicService.class));
|
||||
}
|
||||
Log.d(TAG, "onCheckedChanged:" + isChecked);
|
||||
});
|
||||
}
|
||||
|
||||
//接口请求失败重试时间间隔
|
||||
private void editRetryDelayTime(final EditText et_retry_times, final EditText et_delay_time) {
|
||||
et_retry_times.setText(String.valueOf(SettingUtil.getRetryTimes()));
|
||||
@ -945,4 +968,48 @@ public class SettingActivity extends AppCompatActivity {
|
||||
return super.onMenuOpened(featureId, menu);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前手机品牌
|
||||
*
|
||||
* @return 手机品牌
|
||||
*/
|
||||
public static String getAutoStartTips() {
|
||||
String brand = Build.BRAND.toLowerCase();
|
||||
String tips;
|
||||
|
||||
switch (brand) {
|
||||
case "huawei":
|
||||
tips = "华为手机:应用启动管理 -> 关闭应用开关 -> 打开允许自启动";
|
||||
break;
|
||||
case "honor":
|
||||
tips = "荣耀手机:应用启动管理 -> 关闭应用开关 -> 打开允许自启动";
|
||||
break;
|
||||
case "xiaomi":
|
||||
tips = "小米手机:授权管理 -> 自启动管理 -> 允许应用自启动";
|
||||
break;
|
||||
case "oppo":
|
||||
tips = "OPPO手机:权限隐私 -> 自启动管理 -> 允许应用自启动";
|
||||
break;
|
||||
case "vivo":
|
||||
tips = "vivo手机:权限管理 -> 自启动 -> 允许应用自启动";
|
||||
break;
|
||||
case "meizu":
|
||||
tips = "魅族手机:权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行";
|
||||
break;
|
||||
case "samsung":
|
||||
tips = "三星手机:自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用";
|
||||
break;
|
||||
case "letv":
|
||||
tips = "乐视手机:自启动管理 -> 允许应用自启动";
|
||||
break;
|
||||
case "smartisan":
|
||||
tips = "锤子手机:权限管理 -> 自启动权限管理 -> 点击应用 -> 允许被系统启动";
|
||||
break;
|
||||
default:
|
||||
tips = "未知手机品牌:需要自主查看设置操作";
|
||||
break;
|
||||
}
|
||||
|
||||
return tips;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,9 @@ import android.util.Log;
|
||||
|
||||
import com.idormy.sms.forwarder.service.BatteryService;
|
||||
import com.idormy.sms.forwarder.service.FrontService;
|
||||
import com.idormy.sms.forwarder.service.MusicService;
|
||||
import com.idormy.sms.forwarder.utils.InitUtil;
|
||||
import com.idormy.sms.forwarder.utils.SettingUtil;
|
||||
|
||||
public class RebootBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
@ -31,6 +33,11 @@ public class RebootBroadcastReceiver extends BroadcastReceiver {
|
||||
//电池状态监听
|
||||
Intent batteryServiceIntent = new Intent(context, BatteryService.class);
|
||||
context.startService(batteryServiceIntent);
|
||||
|
||||
//后台播放无声音乐
|
||||
if (SettingUtil.getPlaySilenceMusic()) {
|
||||
context.startService(new Intent(context, MusicService.class));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,95 @@
|
||||
package com.idormy.sms.forwarder.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import com.idormy.sms.forwarder.R;
|
||||
|
||||
public class MusicService extends Service {
|
||||
|
||||
private final String TAG = "MusicService";
|
||||
|
||||
private MediaPlayer mMediaPlayer;
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
|
||||
private final AudioManager.OnAudioFocusChangeListener mAudioFocusChange = new
|
||||
AudioManager.OnAudioFocusChangeListener() {
|
||||
@Override
|
||||
public void onAudioFocusChange(int focusChange) {
|
||||
switch (focusChange) {
|
||||
case AudioManager.AUDIOFOCUS_GAIN:
|
||||
Log.e(TAG, "AUDIOFOCUS_GAIN");
|
||||
try {
|
||||
startPlayMusic();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case AudioManager.AUDIOFOCUS_LOSS:
|
||||
Log.e(TAG, "AUDIOFOCUS_LOSS");
|
||||
mAudioManager.abandonAudioFocus(mAudioFocusChange);
|
||||
break;
|
||||
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
|
||||
Log.e(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
|
||||
break;
|
||||
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
|
||||
Log.e(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public MusicService() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
// TODO: Return the communication channel to the service.
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
//音标处理
|
||||
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
|
||||
if (mAudioManager != null)
|
||||
mAudioManager.requestAudioFocus(mAudioFocusChange, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
|
||||
|
||||
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.silence);
|
||||
mMediaPlayer.setLooping(true);
|
||||
|
||||
startPlayMusic();
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
||||
private void startPlayMusic() {
|
||||
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
||||
Log.e(TAG, "启动后台播放音乐");
|
||||
mMediaPlayer.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopPlayMusic() {
|
||||
if (mMediaPlayer != null) {
|
||||
Log.e(TAG, "关闭后台播放音乐");
|
||||
mMediaPlayer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
stopPlayMusic();
|
||||
Log.e(TAG, TAG + "---->onDestroy,停止服务");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ public class Define {
|
||||
public static final String SP_MSG_KEY_STRING_ENABLE_APP_NOTIFY = "tsms_msg_key_switch_enable_app_notify";
|
||||
public static final String SP_MSG_KEY_STRING_CANCEL_APP_NOTIFY = "tsms_msg_key_switch_cancel_app_notify";
|
||||
public static final String SP_MSG_KEY_STRING_ENABLE_EXCLUDE_FROM_RECENTS = "tsms_msg_key_switch_enable_exclude_from_recents";
|
||||
public static final String SP_MSG_KEY_STRING_ENABLE_PLAY_SILENCE_MUSIC = "tsms_msg_key_switch_enable_play_silence_music";
|
||||
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_DEVICE_MARK = "tsms_msg_key_string_add_extra_device_mark";
|
||||
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_SIM1 = "tsms_msg_key_string_add_extra_sim1";
|
||||
public static final String SP_MSG_KEY_STRING_ADD_EXTRA_SIM2 = "tsms_msg_key_string_add_extra_sim2";
|
||||
|
@ -93,6 +93,14 @@ public class SettingUtil {
|
||||
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_EXCLUDE_FROM_RECENTS, false);
|
||||
}
|
||||
|
||||
public static void switchPlaySilenceMusic(Boolean enable) {
|
||||
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_STRING_ENABLE_PLAY_SILENCE_MUSIC, enable).apply();
|
||||
}
|
||||
|
||||
public static boolean getPlaySilenceMusic() {
|
||||
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_PLAY_SILENCE_MUSIC, false);
|
||||
}
|
||||
|
||||
public static void switchSmsTemplate(Boolean switchSmsTemplate) {
|
||||
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_SWITCH_SMS_TEMPLATE, switchSmsTemplate).apply();
|
||||
}
|
||||
|
@ -544,12 +544,26 @@
|
||||
android:orientation="horizontal"
|
||||
android:padding="15dp">
|
||||
|
||||
<TextView
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="4"
|
||||
android:text="@string/auto_startup"
|
||||
android:textStyle="bold" />
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/auto_startup"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_auto_startup"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/auto_startup"
|
||||
android:textSize="9sp"
|
||||
tools:ignore="SmallSp" />
|
||||
</LinearLayout>
|
||||
|
||||
<Switch
|
||||
android:id="@+id/switch_with_reboot"
|
||||
@ -645,6 +659,47 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
android:background="@color/setting_bar_color"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:padding="15dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="4"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/play_silence_music"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="RelativeOverlap" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/play_silence_music_tips"
|
||||
android:textSize="9sp"
|
||||
tools:ignore="SmallSp" />
|
||||
</LinearLayout>
|
||||
|
||||
<Switch
|
||||
android:id="@+id/switch_play_silence_music"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="end"
|
||||
android:textSize="16sp"
|
||||
tools:ignore="UseSwitchCompatOrMaterialXml" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
BIN
app/src/main/res/raw/silence.mp3
Normal file
BIN
app/src/main/res/raw/silence.mp3
Normal file
Binary file not shown.
@ -434,4 +434,6 @@
|
||||
<string name="toast_denied_never">被永久拒绝授权,请前往系统设置手动授予权限!</string>
|
||||
<string name="toast_denied">获取必需的权限失败,APP功能可能受限!</string>
|
||||
<string name="tips_first_time">\n    首次使用请按照1234步骤顺序设置,数字点亮表示该步骤已设置(4点亮表示有成功日志)!\n\n    根据以往用户反馈,90%的新用户最终排查结果都是手机权限设置问题,5%的用户是APP保活问题,其他问题排查参考Wiki中的常见问题(APP右上角使用帮助入口)\n\n    强烈建议:手动排查一下系统设置:自启动、电源策略、APP的权限,把所有权限打开(始终允许,不要“使用中允许”,MIUI系统就全部变绿),特别是一些国产定制系统加入隐私保护、验证码保护等(例如:MIUI 系统的 空白通行证)\n\n    请选择是否立即“前往系统设置”检查,或“稍后自行处理”?</string>
|
||||
<string name="play_silence_music">Play silent music in the background</string>
|
||||
<string name="play_silence_music_tips">Play silent music to keep running in the background, which may consume more power, enable it on demand</string>
|
||||
</resources>
|
||||
|
@ -327,7 +327,7 @@
|
||||
<string name="battery_monitor">电池监控</string>
|
||||
<string name="battery_monitor_tips">【注意】需要手动创建APP转发规则,包名:88888888</string>
|
||||
<string name="keep_alive">保活措施</string>
|
||||
<string name="keep_alive_tips">建议开启每一项授权或设置,不要禁用通知栏,避免APP被杀</string>
|
||||
<string name="keep_alive_tips">建议开启前三项授权或设置,不要禁用通知栏,避免APP被杀</string>
|
||||
<string name="custom_settings">个性设置</string>
|
||||
<string name="custom_settings_tips">如果自动获取失败,请手动填写备注 或 清空后再次自动获取</string>
|
||||
<string name="times">次</string>
|
||||
@ -433,4 +433,6 @@
|
||||
<string name="toast_denied_never">被永久拒绝授权,请前往系统设置手动授予权限!</string>
|
||||
<string name="toast_denied">获取必需的权限失败,APP功能可能受限!</string>
|
||||
<string name="tips_first_time">\n    首次使用请按照1234步骤顺序设置,数字点亮表示该步骤已设置(4点亮表示有成功日志)!\n\n    根据以往用户反馈,90%的新用户最终排查结果都是手机权限设置问题,5%的用户是APP保活问题,其他问题排查参考Wiki中的常见问题(APP右上角使用帮助入口)\n\n    强烈建议:手动排查一下系统设置:自启动、电源策略、APP的权限,把所有权限打开(始终允许,不要“使用中允许”,MIUI系统就全部变绿),特别是一些国产定制系统加入隐私保护、验证码保护等(例如:MIUI 系统的 空白通行证)\n\n    请选择是否立即“前往系统设置”检查,或“稍后自行处理”?</string>
|
||||
<string name="play_silence_music">后台播放无声音乐</string>
|
||||
<string name="play_silence_music_tips">播放无声音乐让后台一直运行,可能比较耗电,按需启用</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user