转载请注明出处:http://blog.csdn.net/droyon/article/details/39893555
1、描述。
if (si != null && si.mServiceInfo.descriptionRes != 0) {
description.setText(getActivity().getPackageManager().getText(
si.mServiceInfo.packageName, si.mServiceInfo.descriptionRes,
si.mServiceInfo.applicationInfo));//
} else {
if (mi.mBackground) {//这是旧应用进程,仍在运行,以备不时之需。通常不建议停止。
description.setText(R.string.background_process_stop_description);
} else if (detail.mManageIntent != null) {
try {
Resources clientr = getActivity().getPackageManager().getResourcesForApplication(
si.mRunningService.clientPackage);
String label = clientr.getString(si.mRunningService.clientLabel);
description.setText(getActivity().getString(R.string.service_manage_description,
label));//目前正在使用“%1$s ”。触摸“设置”可以对其进行控制。
} catch (PackageManager.NameNotFoundException e) {
}
} else {
description.setText(getActivity().getText(si != null
? R.string.service_stop_description//此服务由其应用启动。停止服务可能会导致应用无法运行。
: R.string.heavy_weight_stop_description));//无法安全地停止该应用。如果强行停止,可能会导致您目前的部分工作内容丢失。
}
是否含有manageIntent。
detail.mManageIntent = mAm.getRunningServiceControlPanel(
si.mRunningService.service);
detail.mManageIntent为true,则button显示为“设置”,点击设置,进入相应界面。
如果mManageIntent为false,则显示“停止”.
点击逻辑
if (mManageIntent != null) {
try {设置
getActivity().startIntentSender(mManageIntent.getIntentSender(), null,
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET,
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, 0);
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, e);
} catch (IllegalArgumentException e) {
Log.w(TAG, e);
} catch (ActivityNotFoundException e) {
Log.w(TAG, e);
}
} else if (mServiceItem != null) {
stopActiveService(false);//停止前台服务。
} else if (mActiveItem.mItem.mBackground) {
// Background process. Just kill it.//停止后台线程
mAm.killBackgroundProcesses(mActiveItem.mItem.mPackageInfo.packageName);
finish();
} else {//强制停止运行
// Heavy-weight process. We'll do a force-stop on it.
mAm.forceStopPackage(mActiveItem.mItem.mPackageInfo.packageName);
finish();
}
int enabled = Settings.Global.getInt(getActivity().getContentResolver(),
Settings.Global.SEND_ACTION_APP_ERROR, 0);//此功能是否使能
if (enabled != 0 && si != null) {
detail.mInstaller = ApplicationErrorReport.getErrorReportReceiver(
getActivity(), si.mServiceInfo.packageName,
si.mServiceInfo.applicationInfo.flags);
detail.mReportButton.setEnabled(detail.mInstaller != null);
} else {
detail.mReportButton.setEnabled(false);
}
public static ComponentName getErrorReportReceiver(Context context,
String packageName, int appFlags) {
// check if error reporting is enabled in secure settings
int enabled = Settings.Global.getInt(context.getContentResolver(),
Settings.Global.SEND_ACTION_APP_ERROR, 0);
if (enabled == 0) {
return null;
}
PackageManager pm = context.getPackageManager();
// look for receiver in the installer package
String candidate = pm.getInstallerPackageName(packageName);//得到packageName所对应的apk来自的安装市场
ComponentName result = getErrorReportReceiver(pm, packageName, candidate);
if (result != null) {
return result;
}
// if the error app is on the system image, look for system apps//如果此app来自system img,则从system app中进行查找
// error receiver
if ((appFlags&ApplicationInfo.FLAG_SYSTEM) != 0) {
candidate = SystemProperties.get(SYSTEM_APPS_ERROR_RECEIVER_PROPERTY);//ro.error.receiver.system.apps
result = getErrorReportReceiver(pm, packageName, candidate);
if (result != null) {
return result;
}
}
// if there is a default receiver, try that
candidate = SystemProperties.get(DEFAULT_ERROR_RECEIVER_PROPERTY);//ro.error.receiver.default
return getErrorReportReceiver(pm, packageName, candidate);
}
static ComponentName getErrorReportReceiver(PackageManager pm, String errorPackage,
String receiverPackage) {
if (receiverPackage == null || receiverPackage.length() == 0) {
return null;
}
// break the loop if it's the error report receiver package that crashed
if (receiverPackage.equals(errorPackage)) {
return null;
}
Intent intent = new Intent(Intent.ACTION_APP_ERROR);
intent.setPackage(receiverPackage);
ResolveInfo info = pm.resolveActivity(intent, 0);
if (info == null || info.activityInfo == null) {
return null;
}
return new ComponentName(receiverPackage, info.activityInfo.name);
}
点击逻辑:
if (v == mReportButton) {
ApplicationErrorReport report = new ApplicationErrorReport();
report.type = ApplicationErrorReport.TYPE_RUNNING_SERVICE;
report.packageName = mServiceItem.mServiceInfo.packageName;
report.installerPackageName = mInstaller.getPackageName();
report.processName = mServiceItem.mRunningService.process;
report.time = System.currentTimeMillis();
report.systemApp = (mServiceItem.mServiceInfo.applicationInfo.flags
& ApplicationInfo.FLAG_SYSTEM) != 0;
ApplicationErrorReport.RunningServiceInfo info
= new ApplicationErrorReport.RunningServiceInfo();
if (mActiveItem.mFirstRunTime >= 0) {
info.durationMillis = SystemClock.elapsedRealtime()-mActiveItem.mFirstRunTime;
} else {
info.durationMillis = -1;
}
ComponentName comp = new ComponentName(mServiceItem.mServiceInfo.packageName,
mServiceItem.mServiceInfo.name);
File filename = getActivity().getFileStreamPath("service_dump.txt");
FileOutputStream output = null;
try {
output = new FileOutputStream(filename);
Debug.dumpService("activity", output.getFD(),
new String[] { "-a", "service", comp.flattenToString() });
} catch (IOException e) {
Log.w(TAG, "Can't dump service: " + comp, e);
} finally {
if (output != null) try { output.close(); } catch (IOException e) {}
}
FileInputStream input = null;
try {
input = new FileInputStream(filename);
byte[] buffer = new byte[(int) filename.length()];
input.read(buffer);
info.serviceDetails = new String(buffer);
} catch (IOException e) {
Log.w(TAG, "Can't read service dump: " + comp, e);
} finally {
if (input != null) try { input.close(); } catch (IOException e) {}
}
filename.delete();
Log.i(TAG, "Details: " + info.serviceDetails);
report.runningServiceInfo = info;
Intent result = new Intent(Intent.ACTION_APP_ERROR);
result.setComponent(mInstaller);
result.putExtra(Intent.EXTRA_BUG_REPORT, report);
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(result);
return;
}