Example usage for android.content Intent setPackage

List of usage examples for android.content Intent setPackage

Introduction

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

Prototype

public @NonNull Intent setPackage(@Nullable String packageName) 

Source Link

Document

(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to.

Usage

From source file:com.nick.scalpel.core.AutoBindWirer.java

@Override
public void wire(final Context context, final Object object, final Field field) {
    ReflectionUtils.makeAccessible(field);
    Object fieldObject = ReflectionUtils.getField(field, object);
    if (fieldObject != null)
        return;// w w  w  . ja v a  2  s.co  m

    // FIXME: 21/03/16 Ensure it is an AIDL service.
    boolean isIInterface = field.getType().isInterface();
    Preconditions.checkState(isIInterface,
            "Field:" + field.getName() + " is not an AIDL interface, is:" + field.getType());

    AutoBind autoBind = field.getAnnotation(AutoBind.class);
    String action = autoBind.action();
    String pkg = autoBind.pkg();
    int flags = autoBind.flags();
    String callback = autoBind.callback();
    boolean startService = autoBind.startService();
    boolean isExplicit = !TextUtils.isEmpty(action) && !TextUtils.isEmpty(pkg);
    Preconditions.checkState(isExplicit, "Action and PackageName should be specified");

    boolean autoUnbind = autoBind.autoUnbind();
    boolean isActivity = object instanceof Activity;
    Preconditions.checkState(!autoUnbind || isActivity, "Auto unbind only work for activities.");

    AutoBind.Callback callbackInstance = null;
    if (!TextUtils.isEmpty(callback)) {
        Object callbackObject = null;
        switch (callback) {
        case "this":
            callbackObject = object;
            break;
        default:
            Field callbackField = ReflectionUtils.findField(object, callback);
            if (callbackField != null) {
                ReflectionUtils.makeAccessible(callbackField);
                callbackObject = ReflectionUtils.getField(callbackField, object);
            }
        }
        boolean isCallback = callbackObject instanceof AutoBind.Callback;
        Preconditions.checkState(isCallback, "Field:" + callback + " is not an instance of Callback.");
        callbackInstance = (AutoBind.Callback) callbackObject;
    }

    final Intent intent = new Intent(action);
    intent.setPackage(pkg);

    if (startService)
        context.startService(intent);

    final AutoBind.Callback finalCallbackInstance = callbackInstance;
    final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Class serviceClass = field.getType();
            String stubClassName = serviceClass.getName() + "$Stub";
            try {
                Class stubClass = Class.forName(stubClassName);
                Method asInterface = ReflectionUtils.findMethod(stubClass, "asInterface", IBinder.class);
                Object result = ReflectionUtils.invokeMethod(asInterface, null, service);
                ReflectionUtils.setField(field, object, result);
                // Callback result.
                if (finalCallbackInstance != null)
                    finalCallbackInstance.onServiceBound(name, this, intent);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (finalCallbackInstance != null)
                finalCallbackInstance.onServiceDisconnected(name);
        }
    };
    //noinspection ResourceType
    context.bindService(intent, connection, flags);

    if (autoUnbind) {
        final String fieldName = field.getName();
        boolean registered = mLifeCycleManager
                .registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
                    @Override
                    public void onActivityDestroyed(Activity activity) {
                        super.onActivityDestroyed(activity);
                        if (activity == object) {
                            logV("unBind service for: " + fieldName);
                            context.unbindService(connection);
                            mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
                        }
                    }
                });
        if (!registered) {
            logE("Failed to register life cycle callback!");
        }
    }
}

From source file:nu.yona.app.api.service.ActivityMonitorService.java

private void restartService() {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1,
            restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + AppConstant.ONE_SECOND,
            restartServicePendingIntent);

}

From source file:org.mariotaku.twidere.activity.SettingsActivity.java

@Override
public boolean onPreferenceClick(final Preference preference) {
    final String key = preference.getKey();
    final Bundle args = new Bundle();
    final int res_id;
    if (KEY_CUSTOM_TABS.equals(key)) {
        if (isDualPaneMode()) {
            showFragment(new CustomTabsFragment());
        } else {/*  w  w w . jav  a2  s .  c  om*/
            final Intent intent = new Intent(INTENT_ACTION_CUSTOM_TABS);
            intent.setPackage(getPackageName());
            startActivity(intent);
        }
        return true;
    } else if (KEY_EXTENSIONS.equals(key)) {
        if (isDualPaneMode()) {
            showFragment(new ExtensionsListFragment());
        } else {
            final Intent intent = new Intent(INTENT_ACTION_EXTENSIONS);
            intent.setPackage(getPackageName());
            startActivity(intent);
        }
        return true;
    } else if (KEY_FILTERS.equals(key)) {
        if (isDualPaneMode()) {
            final FragmentManager fm = getSupportFragmentManager();
            if (fm.findFragmentById(PANE_RIGHT) instanceof FiltersListFragment) {
                showRightPane();
                return true;
            }
            showFragment(new FiltersListFragment());
        } else {
            final Intent intent = new Intent(INTENT_ACTION_FILTERS);
            intent.setClass(this, FiltersActivity.class);
            startActivity(intent);
        }
        return true;
    } else if (KEY_SETTINGS_APPEARANCE.equals(key)) {
        res_id = R.xml.settings_appearance;
    } else if (KEY_SETTINGS_CONTENT_AND_STORAGE.equals(key)) {
        res_id = R.xml.settings_content_and_storage;
    } else if (KEY_SETTINGS_NETWORK.equals(key)) {
        res_id = R.xml.settings_network;
    } else if (KEY_SETTINGS_REFRESH_AND_NOTIFICATIONS.equals(key)) {
        res_id = R.xml.settings_refresh_and_notifications;
    } else if (KEY_SETTINGS_OTHER.equals(key)) {
        res_id = R.xml.settings_other;
    } else if (KEY_ABOUT.equals(key)) {
        res_id = R.xml.about;
    } else {
        res_id = -1;
    }
    if (res_id > 0) {
        args.putInt(INTENT_KEY_RESID, res_id);
        if (isDualPaneMode()) {
            final Fragment fragment = new SettingsDetailsFragment();
            fragment.setArguments(args);
            showFragment(fragment);
        } else {
            final Intent intent = new Intent(this, SettingsDetailsActivity.class);
            intent.putExtras(args);
            startActivity(intent);
        }
    }
    return true;
}

From source file:com.dwdesign.tweetings.activity.SettingsActivity.java

@Override
public boolean onPreferenceClick(final Preference preference) {
    final String key = preference.getKey();
    final Bundle args = new Bundle();
    final int res_id;
    if (KEY_CUSTOM_TABS.equals(key)) {
        if (isDualPaneMode()) {
            final Fragment fragment = new CustomTabsFragment();
            showFragment(fragment, true);
        } else {/*from   ww w  .ja  va2  s  .  c  o  m*/
            final Intent intent = new Intent(INTENT_ACTION_CUSTOM_TABS);
            intent.setPackage(getPackageName());
            startActivity(intent);
        }
        return true;
    } else if (KEY_ABOUT.equals(key)) {
        if (isDualPaneMode()) {
            res_id = R.xml.about;
        } else {
            final Intent intent = new Intent(INTENT_ACTION_ABOUT);
            intent.setPackage(getPackageName());
            startActivity(intent);
            return true;
        }
    } else if (KEY_EXTENSIONS.equals(key)) {
        if (isDualPaneMode()) {
            final Fragment fragment = new ExtensionsListFragment();
            showFragment(fragment, true);
        } else {
            final Intent intent = new Intent(INTENT_ACTION_EXTENSIONS);
            intent.setPackage(getPackageName());
            startActivity(intent);
        }
        return true;
    } else if (KEY_SETTINGS_APPEARANCE.equals(key)) {
        res_id = R.xml.settings_appearance;
    } else if (KEY_SETTINGS_CONTENT_AND_STORAGE.equals(key)) {
        res_id = R.xml.settings_content_and_storage;
    } else if (KEY_SETTINGS_NETWORK.equals(key)) {
        res_id = R.xml.settings_network;
    } else if (KEY_SETTINGS_COMPOSE.equals(key)) {
        res_id = R.xml.settings_compose;
    } else if (KEY_SETTINGS_REFRESH_AND_NOTIFICATIONS.equals(key)) {
        res_id = R.xml.settings_refresh_and_notifications;
    } else if (KEY_SETTINGS_WIDGET.equals(key)) {
        res_id = R.xml.settings_widget;
    } else if (KEY_SETTINGS_OTHER.equals(key)) {
        res_id = R.xml.settings_other;
    } else {
        res_id = -1;
    }
    if (res_id > 0) {
        args.putInt(INTENT_KEY_RESID, res_id);
        if (isDualPaneMode()) {
            final Fragment fragment = new SettingsDetailsFragment();
            fragment.setArguments(args);
            showFragment(fragment, true);
        } else {
            final Intent intent = new Intent(this, SettingsDetailsActivity.class);
            intent.putExtras(args);
            startActivity(intent);
        }
    }
    return true;
}

From source file:com.cloudstudio.BarcodeScanner.java

/**
 * Starts an intent to scan and decode a barcode.
 *//*from  w ww.  j  a va  2s  .c o m*/
public void scan() {
    Intent intentScan = new Intent(SCAN_INTENT);
    intentScan.addCategory(Intent.CATEGORY_DEFAULT);
    // avoid calling other phonegap apps
    intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());

    this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}

From source file:com.hacktx.android.activities.MainActivity.java

private void setupAppShortcuts() {
    // Setup app shortcuts
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1
            && mConfigManager.getValue(ConfigParam.CHECK_IN)) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        Intent checkInIntent = new Intent(Intent.ACTION_VIEW);
        checkInIntent.setPackage("com.hacktx.android");
        checkInIntent.setClass(this, CheckInActivity.class);
        checkInIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY
                | Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        checkInIntent.putExtra("fromShortcut", true);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "check-in")
                .setShortLabel(getString(R.string.app_shortcut_check_in))
                .setLongLabel(getString(R.string.app_shortcut_check_in))
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(checkInIntent).build();

        shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
    }/*from   w ww .  ja va 2 s.  c  om*/
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

public static void openGooglePlayForSpecialPage(Context context, String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (isPackageInstalled(context, "com.android.vending")) {
        intent.setPackage("com.android.vending");
        context.startActivity(intent);//from w  w  w.j  a  v a 2  s.c om
    } else {
        context.startActivity(intent);
    }
}

From source file:org.apache.cordova.plugin.Myket.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("details")) {
        try {// w ww . j  av a  2  s .c o  m
            packageName = args.getString(0);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://myket.ir/app/" + packageName));
            intent.setPackage("ir.mservices.market");
            this.cordova.getActivity().startActivity(intent);
            callbackContext.success("Details Intent Sent");
            return true;
        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }
    if (action.equals("developer")) {
        try {
            packageName = args.getString(0);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://myket.ir/developer/" + packageName + "/apps"));
            intent.setPackage("ir.mservices.market");
            this.cordova.getActivity().startActivity(intent);
            callbackContext.success("Developer Intent Sent");
            return true;
        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    if (action.equals("comment")) {
        try {
            packageName = args.getString(0);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("myket://comment?id=" + packageName));
            intent.setPackage("ir.mservices.market");
            this.cordova.getActivity().startActivity(intent);
            callbackContext.success("Comment Intent Sent");
            return true;
        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    if (action.equals("download")) {
        try {
            packageName = args.getString(0);
            refId = args.getString(1);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("myket://download/" + packageName + "?refId=" + refId));
            intent.setPackage("ir.mservices.market");
            this.cordova.getActivity().startActivity(intent);
            callbackContext.success("Download Intent Sent");
            return true;
        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    if (action.equals("video")) {
        try {
            packageName = args.getString(0);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("myket://video?id=" + packageName));
            intent.setPackage("ir.mservices.market");
            this.cordova.getActivity().startActivity(intent);
            callbackContext.success("Video Intent Sent");
            return true;
        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    return false;
}

From source file:net.gcompris.GComprisActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    intent.setPackage("com.android.vending");
    bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.vladstirbu.cordova.CDVInstagramPlugin.java

private void share(String imageString) {
    if (imageString != null && imageString.length() > 0) {
        byte[] imageData = Base64.decode(imageString, 0);

        FileOutputStream os = null;

        File filePath = new File(this.webView.getContext().getExternalFilesDir(null), "instagram.png");

        try {/*from w  w  w .ja  va  2 s.co  m*/
            os = new FileOutputStream(filePath, true);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            os.write(imageData);
            os.flush();
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
        shareIntent.setPackage("com.instagram.android");

        this.cordova.startActivityForResult((CordovaPlugin) this, shareIntent, 12345);

    } else {
        this.cbContext.error("Expected one non-empty string argument.");
    }
}