mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 01:17:41 +08:00
增加低电量预警
This commit is contained in:
parent
dda91226af
commit
e11123de07
@ -2,9 +2,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.idormy.sms.forwarder">
|
||||
<uses-permission
|
||||
android:name="android.permission.BATTERY_STATS"
|
||||
tools:ignore="ProtectedPermissions" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<!-- 授予应用程序访问系统开机事件的权限 -->
|
||||
@ -22,6 +19,8 @@
|
||||
<!--Android 9(API 级别 28)或更高版本并使用前台服务,则其必须请求 FOREGROUND_SERVICE 权限-->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
<uses-permission android:name="android.permission.BATTERY_STATS"
|
||||
tools:ignore="ProtectedPermissions" />
|
||||
|
||||
<application
|
||||
android:name=".MyApplication"
|
||||
@ -78,8 +77,7 @@
|
||||
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service android:name=".FrontService" />
|
||||
<service android:name=".service.FrontService" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -6,7 +6,7 @@ import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import com.idormy.sms.forwarder.FrontService;
|
||||
import com.idormy.sms.forwarder.service.FrontService;
|
||||
import com.idormy.sms.forwarder.utils.InitUtil;
|
||||
|
||||
public class RebootBroadcastReceiver extends BroadcastReceiver {
|
||||
@ -26,6 +26,9 @@ public class RebootBroadcastReceiver extends BroadcastReceiver {
|
||||
} else {
|
||||
context.startService(frontServiceIntent);
|
||||
}
|
||||
//监控当前电量
|
||||
//Intent batteryServiceIntent = new Intent(context, BatteryService.class);
|
||||
//context.startService(batteryServiceIntent);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -91,13 +91,6 @@ public class SmsForwarderBroadcastReceiver extends BroadcastReceiver {
|
||||
Log.e(TAG, "解析短信失败:" + throwable.getMessage());
|
||||
}
|
||||
|
||||
} else if (intent.ACTION_BATTERY_CHANGED.equals(receiveAction)) {
|
||||
//获得当前电量
|
||||
intLevel = intent.getIntExtra("level", 0);
|
||||
//获得手机总电量
|
||||
intScale = intent.getIntExtra("scale", 100);
|
||||
int percent = intLevel * 100 / intScale;
|
||||
Log.d(TAG, "当前电量:" + percent + "%");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,9 @@ import android.widget.AdapterView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.idormy.sms.forwarder.BroadCastReceiver.SmsForwarderBroadcastReceiver;
|
||||
import com.idormy.sms.forwarder.adapter.LogAdapter;
|
||||
import com.idormy.sms.forwarder.model.vo.LogVo;
|
||||
@ -31,9 +34,6 @@ import com.umeng.analytics.MobclickAgent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements ReFlashListView.IReflashListener {
|
||||
|
||||
ServiceConnection conn = new ServiceConnection() {
|
||||
@ -203,6 +203,7 @@ public class MainActivity extends AppCompatActivity implements ReFlashListView.I
|
||||
builder.setPositiveButton("重发", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
System.out.println(logVo.toString());
|
||||
Long id = logVo.getId();
|
||||
Log.d(TAG, "id = " + id);
|
||||
Log.d(TAG, logVo.toString());
|
||||
@ -299,7 +300,7 @@ public class MainActivity extends AppCompatActivity implements ReFlashListView.I
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
MobclickAgent.onResume(this);
|
||||
//第一次打开,申请权限前无法获取SIM信息,尝试在此重新获取
|
||||
//第一次打开,申请权限前无法获取SIM信息,尝试在此重新获取
|
||||
if (MyApplication.SimInfoList.isEmpty()) {
|
||||
MyApplication.SimInfoList = PhoneUtils.getSimMultiInfo();
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.idormy.sms.forwarder.sender.SendHistory;
|
||||
import com.idormy.sms.forwarder.service.FrontService;
|
||||
import com.idormy.sms.forwarder.utils.Define;
|
||||
import com.idormy.sms.forwarder.utils.PhoneUtils;
|
||||
import com.idormy.sms.forwarder.utils.SettingUtil;
|
||||
|
@ -38,6 +38,9 @@ public class SettingActivity extends AppCompatActivity {
|
||||
EditText et_add_extra_sim2 = (EditText) findViewById(R.id.et_add_extra_sim2);
|
||||
editAddExtraSim2(et_add_extra_sim2);
|
||||
|
||||
EditText et_battery_level_alarm = (EditText) findViewById(R.id.et_battery_level_alarm);
|
||||
editBatteryLevelAlarm(et_battery_level_alarm);
|
||||
|
||||
Switch switch_sms_template = (Switch) findViewById(R.id.switch_sms_template);
|
||||
switchSmsTemplate(switch_sms_template);
|
||||
|
||||
@ -124,6 +127,26 @@ public class SettingActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
//设置低电量报警值
|
||||
private void editBatteryLevelAlarm(final EditText et_battery_level_alarm) {
|
||||
et_battery_level_alarm.setText(SettingUtil.getBatteryLevelAlarm());
|
||||
|
||||
et_battery_level_alarm.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
SettingUtil.setBatteryLevelAlarm(et_battery_level_alarm.getText().toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//设置转发时启用自定义模版
|
||||
private void switchSmsTemplate(Switch switch_sms_template) {
|
||||
boolean isOn = SettingUtil.getSwitchSmsTemplate();
|
||||
|
@ -0,0 +1,108 @@
|
||||
package com.idormy.sms.forwarder.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class BatteryService extends Service {
|
||||
|
||||
private static final String TAG = "BatteryReceiver";
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Log.i(TAG, "onCreate--------------");
|
||||
IntentFilter batteryfilter = new IntentFilter();
|
||||
batteryfilter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||||
registerReceiver(batteryReceiver, batteryfilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId) {
|
||||
super.onStart(intent, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.i(TAG, "onStartCommand--------------");
|
||||
return Service.START_STICKY; //保证service不被杀死
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.i(TAG, "onDestroy--------------");
|
||||
super.onDestroy();
|
||||
this.unregisterReceiver(batteryReceiver);
|
||||
|
||||
}
|
||||
|
||||
// 接收电池信息更新的广播
|
||||
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.i(TAG, "BatteryReceiver--------------");
|
||||
String action = intent.getAction();
|
||||
Log.i(TAG, " 0 action:" + action);
|
||||
Log.i(TAG, "ACTION_BATTERY_CHANGED");
|
||||
int status = intent.getIntExtra("status", 0);
|
||||
int health = intent.getIntExtra("health", 0);
|
||||
boolean present = intent.getBooleanExtra("present", false);
|
||||
int level = intent.getIntExtra("level", 0);
|
||||
int scale = intent.getIntExtra("scale", 0);
|
||||
int icon_small = intent.getIntExtra("icon-small", 0);
|
||||
int plugged = intent.getIntExtra("plugged", 0);
|
||||
int voltage = intent.getIntExtra("voltage", 0);
|
||||
int temperature = intent.getIntExtra("temperature", 0);
|
||||
String technology = intent.getStringExtra("technology");
|
||||
|
||||
String statusString = "";
|
||||
switch (status) {
|
||||
case BatteryManager.BATTERY_STATUS_UNKNOWN:
|
||||
statusString = "unknown";
|
||||
break;
|
||||
case BatteryManager.BATTERY_STATUS_CHARGING:
|
||||
statusString = "charging";
|
||||
break;
|
||||
case BatteryManager.BATTERY_STATUS_DISCHARGING:
|
||||
statusString = "discharging";
|
||||
break;
|
||||
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
|
||||
statusString = "not charging";
|
||||
break;
|
||||
case BatteryManager.BATTERY_STATUS_FULL:
|
||||
statusString = "full";
|
||||
break;
|
||||
}
|
||||
String acString = "";
|
||||
|
||||
switch (plugged) {
|
||||
case BatteryManager.BATTERY_PLUGGED_AC:
|
||||
acString = "plugged ac";
|
||||
break;
|
||||
case BatteryManager.BATTERY_PLUGGED_USB:
|
||||
acString = "plugged usb";
|
||||
break;
|
||||
}
|
||||
|
||||
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS ");
|
||||
String date = sDateFormat.format(new java.util.Date());
|
||||
|
||||
Log.i(TAG, "battery: date=" + date + ",status " + statusString
|
||||
+ ",level=" + level + ",scale=" + scale
|
||||
+ ",voltage=" + voltage + ",acString=" + acString);
|
||||
|
||||
}
|
||||
};
|
||||
}
|
@ -1,19 +1,37 @@
|
||||
package com.idormy.sms.forwarder;
|
||||
package com.idormy.sms.forwarder.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.idormy.sms.forwarder.MainActivity;
|
||||
import com.idormy.sms.forwarder.MyApplication;
|
||||
import com.idormy.sms.forwarder.R;
|
||||
import com.idormy.sms.forwarder.model.LogModel;
|
||||
import com.idormy.sms.forwarder.model.SenderModel;
|
||||
import com.idormy.sms.forwarder.model.vo.SmsVo;
|
||||
import com.idormy.sms.forwarder.sender.SendUtil;
|
||||
import com.idormy.sms.forwarder.sender.SenderUtil;
|
||||
import com.idormy.sms.forwarder.utils.LogUtil;
|
||||
import com.idormy.sms.forwarder.utils.OSUtils;
|
||||
import com.idormy.sms.forwarder.utils.PhoneUtils;
|
||||
import com.idormy.sms.forwarder.utils.SettingUtil;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
|
||||
public class FrontService extends Service {
|
||||
@ -66,6 +84,37 @@ public class FrontService extends Service {
|
||||
PhoneUtils.init(this);
|
||||
MyApplication.SimInfoList = PhoneUtils.getSimMultiInfo();
|
||||
}
|
||||
|
||||
// 低电量预警
|
||||
final int[] alarmTimes = {0}; //通知次数,只通知2次
|
||||
SenderUtil.init(this);
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
int batteryLevel = getBatteryLevel();
|
||||
System.out.println("当前剩余电量:" + batteryLevel + "%");
|
||||
int batteryLevelAlarm = Integer.parseInt(SettingUtil.getBatteryLevelAlarm());
|
||||
System.out.println(alarmTimes[0]);
|
||||
if (alarmTimes[0] <= 1 && batteryLevelAlarm > 0 && batteryLevelAlarm <= 100 && batteryLevel == batteryLevelAlarm) {
|
||||
Date date = new Date();
|
||||
String msg = "当前剩余电量:" + batteryLevel + "%,已经到达低电量预警阈值,请及时充电!";
|
||||
System.out.println(msg);
|
||||
SmsVo smsVo = new SmsVo("888888", msg, date, "");
|
||||
List<SenderModel> senderModels = SenderUtil.getSender(null, null);
|
||||
for (SenderModel senderModel : senderModels
|
||||
) {
|
||||
long ruleId = 0;
|
||||
long logId = LogUtil.addLog(new LogModel(smsVo.getMobile(), smsVo.getContent(), smsVo.getSimInfo(), ruleId));
|
||||
SendUtil.senderSendMsgNoHandError(smsVo, senderModel, logId);
|
||||
}
|
||||
alarmTimes[0] = alarmTimes[0] + 1;
|
||||
}
|
||||
|
||||
if (batteryLevelAlarm > 0 && batteryLevelAlarm <= 100 && batteryLevel > batteryLevelAlarm) {
|
||||
alarmTimes[0] = 0;
|
||||
}
|
||||
}
|
||||
}, 0, 10000);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@ -77,7 +126,19 @@ public class FrontService extends Service {
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.i(TAG, "flags: " + flags + " startId: " + startId);
|
||||
return START_STICKY;
|
||||
return START_STICKY; //保证service不被杀死
|
||||
}
|
||||
|
||||
//获取当前电量
|
||||
private int getBatteryLevel() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
|
||||
return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
|
||||
} else {
|
||||
Intent intent = new ContextWrapper(getApplicationContext()).
|
||||
registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
return (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /
|
||||
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
}
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ public class Define {
|
||||
public static String SP_MSG_KEY_STRING_ADD_EXTRA_SIM2 = "tsms_msg_key_string_add_extra_sim2";
|
||||
public static String SP_MSG_KEY_SWITCH_SMS_TEMPLATE = "tsms_msg_key_switch_sms_template";
|
||||
public static String SP_MSG_KEY_STRING_SMS_TEMPLATE = "tsms_msg_key_string_sms_template";
|
||||
public static String SP_MSG_KEY_STRING_BATTERY_LEVEL_ALARM = "tsms_msg_key_string_battery_level_alarm";
|
||||
|
||||
public static String SP_MSG = "forwarder_msg";
|
||||
public static String SP_MSG_SET_KEY = "forwarder_msg_set_key";
|
||||
|
@ -99,6 +99,18 @@ public class SettingUtil {
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static String getBatteryLevelAlarm() {
|
||||
String res = sp_setting.getString(Define.SP_MSG_KEY_STRING_BATTERY_LEVEL_ALARM, "");
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void setBatteryLevelAlarm(String battery_level) {
|
||||
Log.d(TAG, "battery_level :" + battery_level);
|
||||
sp_setting.edit()
|
||||
.putString(Define.SP_MSG_KEY_STRING_BATTERY_LEVEL_ALARM, battery_level)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static boolean saveMsgHistory() {
|
||||
return sp_setting.getBoolean("option_save_history_on", false);
|
||||
}
|
||||
|
@ -113,6 +113,46 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
android:background="@android:color/white"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal"
|
||||
android:padding="15dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="低电量预警阈值"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="end"
|
||||
android:text="取值范围:0-100,留空或0禁用"
|
||||
android:textSize="9dp" />
|
||||
</LinearLayout>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_battery_level_alarm"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="5dp"
|
||||
android:ems="16"
|
||||
android:inputType="number"
|
||||
android:maxLines="1"
|
||||
android:text="" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
Loading…
x
Reference in New Issue
Block a user