Example usage for android.content Intent resolveTypeIfNeeded

List of usage examples for android.content Intent resolveTypeIfNeeded

Introduction

In this page you can find the example usage for android.content Intent resolveTypeIfNeeded.

Prototype

public @Nullable String resolveTypeIfNeeded(@NonNull ContentResolver resolver) 

Source Link

Document

Return the MIME data type of this intent, only if it will be needed for intent resolution.

Usage

From source file:android.content.BroadcastReceiver.java

/**
 * Provide a binder to an already-running service.  This method is synchronous
 * and will not start the target service if it is not present, so it is safe
 * to call from {@link #onReceive}./*from w w  w .  java 2s  . c  om*/
 * 
 * @param myContext The Context that had been passed to {@link #onReceive(Context, Intent)}
 * @param service The Intent indicating the service you wish to use.  See {@link
 * Context#startService(Intent)} for more information.
 */
public IBinder peekService(Context myContext, Intent service) {
    IActivityManager am = ActivityManagerNative.getDefault();
    IBinder binder = null;
    try {
        service.setAllowFds(false);
        binder = am.peekService(service, service.resolveTypeIfNeeded(myContext.getContentResolver()));
    } catch (RemoteException e) {
    }
    return binder;
}

From source file:android.app.Activity.java

private void startIntentSenderForResultInner(IntentSender intent, int requestCode, Intent fillInIntent,
        int flagsMask, int flagsValues, Activity activity, Bundle options)
        throws IntentSender.SendIntentException {
    try {/*from w w w .java 2s.  c  om*/
        String resolvedType = null;
        if (fillInIntent != null) {
            fillInIntent.migrateExtraStreamToClipData();
            fillInIntent.prepareToLeaveProcess();
            resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
        }
        int result = ActivityManagerNative.getDefault().startActivityIntentSender(
                mMainThread.getApplicationThread(), intent, fillInIntent, resolvedType, mToken,
                activity.mEmbeddedID, requestCode, flagsMask, flagsValues, options);
        if (result == ActivityManager.START_CANCELED) {
            throw new IntentSender.SendIntentException();
        }
        Instrumentation.checkStartActivityResult(result, null);
    } catch (RemoteException e) {
    }
    if (requestCode >= 0) {
        // If this start is requesting a result, we can avoid making
        // the activity visible until the result is received.  Setting
        // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
        // activity hidden during this time, to avoid flickering.
        // This can only be done when a result is requested because
        // that guarantees we will get information back when the
        // activity is finished, no matter what happens to it.
        mStartedActivity = true;
    }
}

From source file:android.app.Activity.java

/**
 * A special variation to launch an activity only if a new activity
 * instance is needed to handle the given Intent.  In other words, this is
 * just like {@link #startActivityForResult(Intent, int)} except: if you are 
 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
 * singleTask or singleTop //from   w  ww  . ja  v  a 2 s.  co  m
 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
 * and the activity 
 * that handles <var>intent</var> is the same as your currently running 
 * activity, then a new instance is not needed.  In this case, instead of 
 * the normal behavior of calling {@link #onNewIntent} this function will 
 * return and you can handle the Intent yourself. 
 * 
 * <p>This function can only be called from a top-level activity; if it is
 * called from a child activity, a runtime exception will be thrown.
 * 
 * @param intent The intent to start.
 * @param requestCode If >= 0, this code will be returned in
 *         onActivityResult() when the activity exits, as described in
 *         {@link #startActivityForResult}.
 * @param options Additional options for how the Activity should be started.
 * See {@link android.content.Context#startActivity(Intent, Bundle)
 * Context.startActivity(Intent, Bundle)} for more details.
 * 
 * @return If a new activity was launched then true is returned; otherwise
 *         false is returned and you must handle the Intent yourself.
 *  
 * @see #startActivity
 * @see #startActivityForResult
 */
public boolean startActivityIfNeeded(Intent intent, int requestCode, Bundle options) {
    if (mParent == null) {
        int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess();
            result = ActivityManagerNative.getDefault().startActivity(mMainThread.getApplicationThread(),
                    getBasePackageName(), intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
                    mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, null, null, options);
        } catch (RemoteException e) {
            // Empty
        }

        Instrumentation.checkStartActivityResult(result, intent);

        if (requestCode >= 0) {
            // If this start is requesting a result, we can avoid making
            // the activity visible until the result is received.  Setting
            // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
            // activity hidden during this time, to avoid flickering.
            // This can only be done when a result is requested because
            // that guarantees we will get information back when the
            // activity is finished, no matter what happens to it.
            mStartedActivity = true;
        }
        return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
    }

    throw new UnsupportedOperationException(
            "startActivityIfNeeded can only be called from a top-level activity");
}