The code behind the Force stop button is complex and undocumented. I'll reproduce it verbatim and then go through it one piece at a time.
private void checkForceStop() {
if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
// User can't force stop device admin.
updateForceStopButton(false);
} else if ((mAppEntry.info.flags&ApplicationInfo.FLAG_STOPPED) == 0) {
// If the app isn't explicitly stopped, then always show the
// force stop button.
updateForceStopButton(true);
} else {
Intent intent = new Intent(Intent.ACTION_QUERY_PACKAGE_RESTART,
Uri.fromParts("package", mAppEntry.info.packageName, null));
intent.putExtra(Intent.EXTRA_PACKAGES, new String[] { mAppEntry.info.packageName });
intent.putExtra(Intent.EXTRA_UID, mAppEntry.info.uid);
intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(mAppEntry.info.uid));
getActivity().sendOrderedBroadcast(intent, null, mCheckKillProcessesReceiver, null,
Activity.RESULT_CANCELED, null, null);
}
}
The first if
line checks if any component of the package is a device admin (that is, if it's checked in the Device administrators list in the Security page of the Settings app). If so, the button is disabled (greyed out) so you can't stop device admins.
Then, the else if
checks if the package is already stopped. Here, stopped doesn't just mean "not running". As far as I can tell, a package is in the stopped state if you've never run any component from it (such as by launching it from the menu), or if you've pressed a Force stop button (this one or in a "stopped working" dialog) and not run any of its components again. If the package hasn't been stopped, the button is enabled.
The else
case covers the rare eventuality that an app has been stopped and isn't a device admin. In this case, it gives other system services a chance to enable the button. For example, the app might have an alarm set and pending, which would later cause the app to be started again even from the stopped state. In this case, the button will be enabled. If no other system service has a reason to enable the button, it will be disabled.
Summary
The Force stop button is enabled if the package is not a device admin and either you haven't already force-stopped this app or you have force-stopped it but a system service thinks it will run it again.
If the app is running (and it isn't a device admin), the Force stop button will definitely be enabled, but the button being enabled doesn't mean the app is running.