android:stopWithTask=";假;但服务确实关闭了
我曾经使用过android:stopWithTask=“false”,但它从未在我的服务中使用过android:stopWithTask=";假;但服务确实关闭了,android,Android,我曾经使用过android:stopWithTask=“false”,但它从未在我的服务中使用过 <service android:name=".EcaService" android:enabled="true" android:stopWithTask="false"/> 代码有什么问题,它没有给出任何错误 你找到解决办法了吗?我也有同样的问题。你找到解决办法了吗?我也有同样的问题。 import androi
<service android:name=".EcaService"
android:enabled="true"
android:stopWithTask="false"/>
代码有什么问题,它没有给出任何错误 你找到解决办法了吗?我也有同样的问题。你找到解决办法了吗?我也有同样的问题。
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;
/**
* Created by ayham on 7/8/2017.
*/
public class EcaService extends Service {
private BroadcastReceiver batteryReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
registerBatteryReceiver();
}
private void registerBatteryReceiver() {
batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (MainView.eca_activeCode < 1) {
// How are we charging?
Log.e("in first if", ": done");
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean wirelessCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (acCharge || wirelessCharge) {
Log.e("in second if", ": done");
Intent do_Alarm = new Intent(context, BatteryAlarmView.class);
do_Alarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainView.eca_activeCode++;
context.startActivity(do_Alarm);
}
}
}
};
IntentFilter electricalAIntent = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, electricalAIntent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("in the receiver","yay");
/*Intent doAlarm = new Intent(this, BatteryAlarmView.class);
doAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(doAlarm);*/
return START_NOT_STICKY;
}
public class MainView extends Activity {
static int eca_activeCode = 0;
Button eca;
Intent eca_intent;
boolean eca_s;
/*@Override
public void onBackPressed() {
Intent itent = new Intent(this, MainView.class);
startActivity(itent);
}*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_view);
this.context = this;
//to be used in decision of the eca
eca_s = false;
//initialize the eca button
eca = (Button)findViewById(R.id.e_status);
//initialize the textView related to the eca
ecaText = (TextView)findViewById(R.id.EText);
//add a click listener to the eca button to start the electrical alarm
eca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//decide whether to activate or not
String s ;
if(!eca_s) {
eca_activeCode = 0;
eca_s = true;
s = "ON";
eca_intent = new Intent(context,EcaService.class);
context.startService(eca_intent);
}
else {
eca_s =false;
s = "Off";
eca_intent = new Intent(context,EcaService.class);
context.stopService(eca_intent);
}
//set the button text to indicate the status of eca
eca.setText(s);
}
});
//receive the battery alarm intent
if(eca_intent != null) {
Intent b_back = getIntent();
eca_s = false;
String s = "Off";
String class_name = b_back.getExtras().getString("Class");
if (class_name.equalsIgnoreCase("BatteryAlarmView")) {
eca_intent = new Intent(context,EcaService.class);
context.stopService(eca_intent);
eca_activeCode = 0;
//set the button text to indicate the status of eca
eca.setText(s);
}
}
}
}