下面列出了android.os.RemoteException#rethrowAsRuntimeException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Disables the service for the specified Activity.
*
* <p>Note that the specified Activity must still be in resumed
* state at the time of this call. A good place to call this method
* is in your {@link Activity#onPause} implementation.
*
* @param activity The activity which the service was registered for
* @return true when successful
*/
public boolean disableService(Activity activity) throws RuntimeException {
if (activity == null) {
throw new NullPointerException("activity is null");
}
if (!activity.isResumed()) {
throw new IllegalArgumentException("Activity must be resumed.");
}
try {
return sService.disableNfcFForegroundService();
} catch (RemoteException e) {
// Try one more time
recoverService();
if (sService == null) {
Log.e(TAG, "Failed to recover CardEmulationService.");
return false;
}
try {
return sService.disableNfcFForegroundService();
} catch (RemoteException ee) {
Log.e(TAG, "Failed to reach CardEmulationService.");
ee.rethrowAsRuntimeException();
return false;
}
}
}
@Override
public void unregisterMoveCallback(MoveCallback callback) {
synchronized (mDelegates) {
for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
final MoveCallbackDelegate delegate = i.next();
if (delegate.mCallback == callback) {
try {
mPM.unregisterMoveCallback(delegate);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
i.remove();
}
}
}
}
@Override
public int movePrimaryStorage(VolumeInfo vol) {
try {
final String volumeUuid;
if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
} else if (vol.isPrimaryPhysical()) {
volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
} else {
volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
}
return mPM.movePrimaryStorage(volumeUuid);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
/**
* Set a NFCID2 for the specified service.
*
* <p>The NFCID2 must be in range from "02FE000000000000" to "02FEFFFFFFFFFFFF".
*
* <p>If a NFCID2 was previously set for this service
* (either statically through the manifest, or dynamically by using this API),
* it will be replaced.
*
* <p>Note that you can only set the NFCID2 for a service that
* is running under the same UID as the caller of this API. Typically
* this means you need to call this from the same
* package as the service itself, though UIDs can also
* be shared between packages using shared UIDs.
*
* @param service The component name of the service
* @param nfcid2 The NFCID2 to be registered
* @return whether the setting was successful.
*/
public boolean setNfcid2ForService(ComponentName service, String nfcid2)
throws RuntimeException {
if (service == null || nfcid2 == null) {
throw new NullPointerException("service or nfcid2 is null");
}
try {
return sService.setNfcid2ForService(mContext.getUserId(),
service, nfcid2);
} catch (RemoteException e) {
// Try one more time
recoverService();
if (sService == null) {
Log.e(TAG, "Failed to recover CardEmulationService.");
return false;
}
try {
return sService.setNfcid2ForService(mContext.getUserId(),
service, nfcid2);
} catch (RemoteException ee) {
Log.e(TAG, "Failed to reach CardEmulationService.");
ee.rethrowAsRuntimeException();
return false;
}
}
}
@Override
public boolean canRequestPackageInstalls() {
try {
return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public String getAppPredictionServicePackageName() {
try {
return mPM.getAppPredictionServicePackageName();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public String getAttentionServicePackageName() {
try {
return mPM.getAttentionServicePackageName();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public String getInstantAppAndroidId(String packageName, UserHandle user) {
try {
return mPM.getInstantAppAndroidId(packageName, user.getIdentifier());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public boolean canRequestPackageInstalls() {
try {
return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public boolean isPackageStateProtected(String packageName, int userId) {
try {
return mPM.isPackageStateProtected(packageName, userId);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
/**
* Any extras sent by the requesting app.
*
* @return For a shortcut request, this method always return null. For an AppWidget
* request, this method returns the extras passed to the
* {@link android.appwidget.AppWidgetManager#requestPinAppWidget(
* ComponentName, Bundle, PendingIntent)} API. See {@link AppWidgetManager} for details.
*/
@Nullable
public Bundle getExtras() {
try {
return mInner.getExtras();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public boolean canRequestPackageInstalls() {
try {
return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public CharSequence getHarmfulAppWarning(String packageName) {
try {
return mPM.getHarmfulAppWarning(packageName, mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
/**
* {@link AppWidgetProviderInfo} sent by the requesting app.
* Always non-null for a {@link #REQUEST_TYPE_APPWIDGET} request, and always null for a
* different request type.
*
* <p>Launcher should not show any configuration activity associated with the provider, and
* assume that the widget is already fully configured. Upon accepting the widget, it should
* pass the widgetId in {@link #accept(Bundle)}.
*
* @return requested {@link AppWidgetProviderInfo} when a request is of the
* {@link #REQUEST_TYPE_APPWIDGET} type. Null otherwise.
*/
@Nullable
public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) {
try {
final AppWidgetProviderInfo info = mInner.getAppWidgetProviderInfo();
if (info == null) {
return null;
}
info.updateDimensions(context.getResources().getDisplayMetrics());
return info;
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public CharSequence getHarmfulAppWarning(String packageName) {
try {
return mPM.getHarmfulAppWarning(packageName, getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public boolean canRequestPackageInstalls() {
try {
return mPM.canRequestPackageInstalls(mContext.getPackageName(), getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public ComponentName getInstantAppInstallerComponent() {
try {
return mPM.getInstantAppInstallerComponent();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public boolean canRequestPackageInstalls() {
try {
return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
@Override
public ComponentName getInstantAppResolverSettingsComponent() {
try {
return mPM.getInstantAppResolverSettingsComponent();
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}
/**
* Gets a URI backed by a {@link DocumentsProvider} that points to the same media
* file as the specified mediaUri. This allows apps who have permissions to access
* media files in Storage Access Framework to perform file operations through that
* on media files.
* <p>
* Note: this method doesn't grant any URI permission. Callers need to obtain
* permission before calling this method. One way to obtain permission is through
* a 3-step process:
* <ol>
* <li>Call {@link android.os.storage.StorageManager#getStorageVolume(File)} to
* obtain the {@link android.os.storage.StorageVolume} of a media file;</li>
*
* <li>Invoke the intent returned by
* {@link android.os.storage.StorageVolume#createAccessIntent(String)} to
* obtain the access of the volume or one of its specific subdirectories;</li>
*
* <li>Check whether permission is granted and take persistent permission.</li>
* </ol>
* @param mediaUri the media URI which document URI is requested
* @return the document URI
*/
public static Uri getDocumentUri(Context context, Uri mediaUri) {
try {
final ContentResolver resolver = context.getContentResolver();
final String path = getFilePath(resolver, mediaUri);
final List<UriPermission> uriPermissions = resolver.getPersistedUriPermissions();
return getDocumentUri(resolver, path, uriPermissions);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
}