I am trying to debug a service of my app with no luck. The launcher opens an activity from which the service can be stopped and started. The service has no problems here (while the activity is open). When the activity is closed, the service should keep running however it doesn't. As soon as I close the activity, the debugger detaches. I need a way to keep the debugger open so that I can see the error that makes the service die.
This is how I am starting my service in the activity:
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked && !isServiceRunning()) {
if (checkPermissions()) {
startService(new Intent(this, MainService.class).setAction(MainService.ACTION_START));
} else {
requestPermissions();
toggle.setChecked(isServiceRunning());
}
} else if (!isChecked && isServiceRunning()) {
stopService(new Intent(this, MainService.class));
}
}
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (manager != null) {
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if("com.package.MainService".equals(service.service.getClassName())) {
return true;
}
}
}
return false;
}
This is most of my service class:
public class MainService extends Service {
public static final String ACTION_START = "START";
public static final String ACTION_FOUND = "FOUND";
public static final String ACTION_RECEIVED = "RECEIVED";
int volume;
final int notificationID = 94729;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ACTION_RECEIVED.equals(intent.getAction())) {
log("Received Message");
Bundle extras = intent.getExtras();
if (extras != null) {
String msg = extras.getString("msg");
String sender = extras.getString("sender");
receivedMessage(msg, sender);
} else {
log("There was no message");
}
} else if(ACTION_FOUND.equals(intent.getAction())) {
log("Received Found");
found();
} else if (ACTION_START.equals(intent.getAction())) {
log("Starting MainService");
setupNotification();
Toast.makeText(this, "Service Running", Toast.LENGTH_LONG).show();
} else {
stopSelf();
}
return START_STICKY;
}
@Override
public void onDestroy() {
log("Destroying MainService");
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
Edit:
Here is my manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MainService" />
<receiver android:name=".BroadcastListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Any help greatly appreciated.
Play the music from the foreground service, this will prevent it from restarting.
Official Android documentation says:
A music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.
Here is an example