【发布时间】:2020-07-07 21:47:01
【问题描述】:
我编写了一个 android 后台服务,它将在单击按钮时启动,并应在单击另一个按钮时停止。我的服务主体是
public class BatteryServices extends Service {
int on;
int off;
int deviceStatus;
IntentFilter intentfilter;
static int batteryLevel;
String mDeviceAddress;
public String notyTxt="";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
try {
on = intent.getIntExtra("on", 0);
off = intent.getIntExtra("off", 100);
mDeviceAddress = intent.getStringExtra("address");
} catch (NullPointerException e) {
e.printStackTrace();
}
if (on > 0)
notyTxt = "Plug will turn off at: " + off + "% and will turn on at: " + on + "%";
else if (off > 0)
notyTxt = "Overnight charge running. Battery will turn off at: " + off + "%";
Log.d("notify ", notyTxt);
try {
SelectOperation.notifyText.setText("" + notyTxt);
NormalbatteryOn_OFF.backgroundLayout.setBackgroundColor(Color.parseColor("#BEC4C4"));
NormalbatteryOn_OFF.serviceTxt.setText(notyTxt);
NormalbatteryOn_OFF.serviceTxt.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.cbOverNight.setEnabled(false);
NormalbatteryOn_OFF.offpercenedittext.setVisibility(View.GONE);
NormalbatteryOn_OFF.offpercentageText.setText("" + off);
NormalbatteryOn_OFF.offpercentageText.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.onpercenedittext.setVisibility(View.GONE);
NormalbatteryOn_OFF.onpercentageText.setText("" + on);
if (on > 0)
NormalbatteryOn_OFF.onpercentageText.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.activatebutton.setVisibility(View.GONE);
NormalbatteryOn_OFF.change.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.onoff_btn.setVisibility(View.GONE);
} catch (NullPointerException e) {
e.printStackTrace();
}
registerReceiver(broadcastreceiver, intentfilter);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service Closed", Toast.LENGTH_SHORT).show();
SelectOperation.notifyText.setText("");
NormalbatteryOn_OFF.serviceTxt.setVisibility(View.GONE);
SharedPreferences.Editor editor = NormalbatteryOn_OFF.preferences.edit();
editor.putString("serviceCheck","");
editor.apply();
try {
NormalbatteryOn_OFF.backgroundLayout.setBackgroundColor(Color.parseColor("#FCE4EC"));
NormalbatteryOn_OFF.cbOverNight.setEnabled(true);
NormalbatteryOn_OFF.offpercenedittext.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.offpercentageText.setVisibility(View.GONE);
NormalbatteryOn_OFF.onpercenedittext.setVisibility(View.VISIBLE);
if (on>0){
NormalbatteryOn_OFF.onpercentageText.setVisibility(View.GONE);
}
NormalbatteryOn_OFF.activatebutton.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.change.setVisibility(View.GONE);
NormalbatteryOn_OFF.onoff_btn.setVisibility(View.VISIBLE);
NormalbatteryOn_OFF.id_ontextview.setVisibility(View.VISIBLE);
} catch (NullPointerException e){
e.printStackTrace();
}
unregisterReceiver(broadcastreceiver);
}
//Battery Percentage.
private BroadcastReceiver broadcastreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
deviceStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
batteryLevel = (int) (((float) level / (float) scale) * 100.0f);
final String action = intent.getAction();
Log.e("battery status","status"+deviceStatus);
try {
if (batteryLevel >= off) {
MyAppApplication.getBLeService().ON_OFF_Logic("smpoff");
if (on==0){
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
SharedPreferences.Editor editor = NormalbatteryOn_OFF.preferences.edit();
editor.putString("serviceCheck","");
editor.apply();
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
System.exit(0);
}
}
}
}, 5000);
}
}
if (batteryLevel <= on)
MyAppApplication.getBLeService().ON_OFF_Logic("smpon");
} catch (Exception e){
e.printStackTrace();
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_DISCHARGING){
Toast.makeText(getApplicationContext(),"Charger disconnected",Toast.LENGTH_SHORT).show();
}
if (deviceStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
Toast.makeText(getApplicationContext(), "Charger connected", Toast.LENGTH_SHORT).show();
}
}
};
}
我正在启动这样的服务
startService(new Intent(getApplicationContext(), BatteryServices.class).putExtra("on",0).putExtra("off",Integer.parseInt(offpercenedittext.getText().toString())).putExtra("address",mDeviceAddress));
这样结束服务
stopService(new Intent(getApplicationContext(),BatteryServices.class));
在清单文件中,我声明了服务
<service android:name=".battery_onoff.BatteryServices"
android:enabled="true"/>
所以我的期望是当我在我的应用程序上使用其他应用程序时,该服务应该运行。但是,当我在一段时间后按下主页按钮(不杀死应用程序)时,服务被破坏了。有什么解决办法吗?
【问题讨论】:
-
请分享您的堆栈跟踪信息,除非操作系统这样做,否则任何服务都不会自行终止,这是许多设备的已知问题
-
感谢您的回复。以下是我的 stcktrace 2020-03-27 12:41:33.634 11161-11305/com.smartplugapp E/BluetoothLeService: onCharacteristicChanged 2020-03-27 12:41:35.095 11161-11161/com.smartplugapp D/InputTransport:输入通道构建: fd=78 2020-03-27 12:41:35.096 11161-11161/com.smartplugapp D/ViewRootImpl@c2cb40[Toast]: setView = android.widget.LinearLayout{5e3a79 VE..... 。一世。 0,0-0,0} TM=true MM=false 2020-03-27 12:41:35.124 11161-11161/com.smartplugapp D/ViewRootImpl@c2cb40[Toast]:Relayout 返回:old=[0,55] [720,1436] 新=[235,1247][485,1324] 结果=0x7 表面={tr
标签: android android-service background-service