下面列出了android.content.Context#startServiceAsUser ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Call EMM agent app to send operation updates.
* @param context - Application context.
* @param operation - Operation code.
* @param operationId - Operation ID.
* @param message - Error message.
*/
public static void callAgentApp(Context context, String operation, int operationId, String message) {
Intent intent = new Intent(Constants.AGENT_APP_SERVICE_NAME);
Intent explicitIntent = createExplicitFromImplicitIntent(context, intent);
if (explicitIntent != null) {
intent = explicitIntent;
}
intent.putExtra("operation", operation);
if (operationId != 0) {
intent.putExtra("id", operationId);
}
if (message != null) {
intent.putExtra("message", message);
}
intent.setPackage(Constants.PACKAGE_NAME);
context.startServiceAsUser(intent, android.os.Process.myUserHandle());
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Network change event triggered.");
if(!Preference.getBoolean(context, FRESH_BOOTUP_FLAG)) {
if (!Preference.getBoolean(context, Constants.PreferenceFlag.REGISTERED) && CommonUtils.
isNetworkAvailable(context)) {
if (Constants.AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED) {
Preference.putBoolean(context, FRESH_BOOTUP_FLAG, true);
Intent autoEnrollIntent = new Intent(context, EnrollmentService.class);
autoEnrollIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
autoEnrollIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startServiceAsUser(autoEnrollIntent, android.os.Process.myUserHandle());
}
CommonUtils.callSystemAppInit(context);
}
}
}
static final void startSystemUi(Context context, WindowManagerService windowManager) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui",
"com.android.systemui.SystemUIService"));
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.SYSTEM);
windowManager.onSystemUiStarted();
}
public static void callSystemAppInit(Context context) {
if(Constants.SYSTEM_APP_ENABLED) {
Intent intent = new Intent(Constants.SYSTEM_APP_SERVICE_START_ACTION);
Intent explicitIntent = createExplicitFromImplicitIntent(context, intent);
if (explicitIntent != null) {
intent = explicitIntent;
}
context.startServiceAsUser(intent, android.os.Process.myUserHandle());
} else {
Log.e(TAG, "System app not enabled.");
}
}
/**
* Call EMM system app in COPE mode.
* @param context - Application context.
* @param operation - Operation code.
* @param command - Shell command to be executed.
* @param appUri - App package/APK URI when an app operation executed.
*/
public static void callSystemApp(Context context, String operation, String command, String appUri) {
if(Constants.SYSTEM_APP_ENABLED) {
Intent intent = new Intent(Constants.SYSTEM_APP_SERVICE_START_ACTION);
Intent explicitIntent = createExplicitFromImplicitIntent(context, intent);
if (explicitIntent != null) {
intent = explicitIntent;
}
intent.putExtra(Constants.OPERATION_CODE, operation);
intent.setPackage(Constants.PACKAGE_NAME);
if (appUri != null) {
intent.putExtra("appUri", appUri);
}
if (command != null) {
if (Constants.Operation.UPGRADE_FIRMWARE.equals(operation)) {
try {
JSONObject upgradeData = new JSONObject(command);
if (upgradeData.isNull(context.getResources()
.getString(R.string.firmware_upgrade_automatic_retry)) && Preference.hasPreferenceKey(context, context
.getResources().getString(R.string.is_automatic_firmware_upgrade))) {
boolean isFirmwareUpgradeAutoRetry = Preference.getBoolean(context, context
.getResources().getString(R.string.is_automatic_firmware_upgrade));
upgradeData.put(context.getResources()
.getString(R.string.firmware_upgrade_automatic_retry), isFirmwareUpgradeAutoRetry);
command = upgradeData.toString();
Log.d(TAG, "Updated payload: " + command);
} else if (!upgradeData.isNull(context.getResources()
.getString(R.string.firmware_upgrade_automatic_retry))){
Preference.putBoolean(context, context.getResources()
.getString(R.string.is_automatic_firmware_upgrade), upgradeData.getBoolean(context.getResources()
.getString(R.string.firmware_upgrade_automatic_retry)));
} else {
upgradeData.put(context.getResources()
.getString(R.string.firmware_upgrade_automatic_retry), true);
Preference.putBoolean(context, context.getResources()
.getString(R.string.is_automatic_firmware_upgrade), true);
Log.d(TAG, "Updated payload: " + command);
}
} catch (JSONException e) {
Log.e(TAG, "Could not parse Firmware upgrade operation", e);
}
intent.putExtra("operationId", Preference.getInt(context, "firmwareOperationId"));
}
intent.putExtra("command", command);
}
context.startServiceAsUser(intent, android.os.Process.myUserHandle());
} else {
Log.e(TAG, "System app not enabled.");
}
}