下面列出了android.app.ActivityManagerNative#getDefault ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setRunAsMonkey(boolean isSet) {
IActivityManager am = ActivityManagerNative.getDefault();
if (am == null) {
throw new RuntimeException(
"Can't manage monkey status; is the system running?");
}
try {
if (isSet) {
am.setActivityController(new DummyActivityController());
} else {
am.setActivityController(null);
}
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
public void initialize(ShellUiAutomatorBridge uiAutomatorBridge) {
mUiAutomationBridge = uiAutomatorBridge;
// 监听activity变化,用于getAct
try {
IActivityManager am = ActivityManagerNative.getDefault();
am.setActivityController(new DummyActivityController());
} catch (RemoteException e) {
}
UiAutomation uiAutomation = uiAutomatorBridge.getUiAutomation();
uiAutomation
.setOnAccessibilityEventListener(new OnAccessibilityEventListener() {
public void onAccessibilityEvent(AccessibilityEvent event) {
synchronized (onAccessibilityEventListeners) {
for (OnAccessibilityEventListener listener : onAccessibilityEventListeners) {
listener.onAccessibilityEvent(event);
}
}
}
});
}
/**
* Evicts a user's CE key by stopping and restarting the user.
*
* The key is evicted automatically by the user controller when the user has stopped.
*/
@Override
public void evictCredentialEncryptionKey(@UserIdInt int userId) {
checkManageUsersPermission("evict CE key");
final IActivityManager am = ActivityManagerNative.getDefault();
final long identity = Binder.clearCallingIdentity();
try {
am.restartUserInBackground(userId);
} catch (RemoteException re) {
throw re.rethrowAsRuntimeException();
} finally {
Binder.restoreCallingIdentity(identity);
}
}
public long getSystemLongPressTime() {
long longPressTimeout = 2000;
try {
IContentProvider provider = null;
Cursor cursor = null;
IActivityManager activityManager = ActivityManagerNative
.getDefault();
String providerName = Settings.Secure.CONTENT_URI.getAuthority();
IBinder token = new Binder();
try {
ContentProviderHolder holder = activityManager
.getContentProviderExternal(providerName,
UserHandle.USER_OWNER, token);
if (holder == null) {
throw new IllegalStateException("Could not find provider: "
+ providerName);
}
provider = holder.provider;
cursor = provider.query(null, Settings.Secure.CONTENT_URI,
new String[] { Settings.Secure.VALUE }, "name=?",
new String[] { Settings.Secure.LONG_PRESS_TIMEOUT },
null, null);
if (cursor.moveToFirst()) {
longPressTimeout = cursor.getInt(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
if (provider != null) {
activityManager.removeContentProviderExternal(providerName,
token);
}
}
} catch (Throwable e) {
}
return longPressTimeout;
}
/**
* Sets the locale specified. Input data is the byte representation of a
* BCP-47 language tag. For backwards compatibility, strings of the form
* {@code ll_CC} are also accepted, where {@code ll} is a two letter language
* code and {@code CC} is a two letter country code.
*
* @param data the locale string in bytes.
*/
void setLocaleData(byte[] data, int size) {
// Check if locale was set by the user:
Configuration conf = mContext.getResources().getConfiguration();
// TODO: The following is not working as intended because the network is forcing a locale
// change after registering. Need to find some other way to detect if the user manually
// changed the locale
if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
final String[] availableLocales = mContext.getAssets().getLocales();
// Replace "_" with "-" to deal with older backups.
String localeCode = new String(data, 0, size).replace('_', '-');
Locale loc = null;
for (int i = 0; i < availableLocales.length; i++) {
if (availableLocales[i].equals(localeCode)) {
loc = Locale.forLanguageTag(localeCode);
break;
}
}
if (loc == null) return; // Couldn't find the saved locale in this version of the software
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
config.locale = loc;
// indicate this isn't some passing default - the user wants this remembered
config.userSetLocale = true;
am.updateConfiguration(config);
} catch (RemoteException e) {
// Intentionally left blank
}
}
public static IActivityManager getAMN() {
return ActivityManagerNative.getDefault();
}
@Override
protected Object getActivityManager() {
Log.i(TAG, "Invoking ActivityManagerNative.getDefault");
return ActivityManagerNative.getDefault();
}