Example usage for android.content Intent setComponent

List of usage examples for android.content Intent setComponent

Introduction

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

Prototype

public @NonNull Intent setComponent(@Nullable ComponentName component) 

Source Link

Document

(Usually optional) Explicitly set the component to handle the intent.

Usage

From source file:com.commonsware.android.advservice.remotebinding.sigcheck.DownloadFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);// w  ww  .  j a va2  s . co  m

    appContext = (Application) getActivity().getApplicationContext();

    Intent implicit = new Intent(IDownload.class.getName());
    List<ResolveInfo> matches = getActivity().getPackageManager().queryIntentServices(implicit, 0);

    if (matches.size() == 0) {
        Toast.makeText(getActivity(), "Cannot find a matching service!", Toast.LENGTH_LONG).show();
    } else if (matches.size() > 1) {
        Toast.makeText(getActivity(), "Found multiple matching services!", Toast.LENGTH_LONG).show();
    } else {
        ServiceInfo svcInfo = matches.get(0).serviceInfo;

        try {
            String otherHash = SignatureUtils.getSignatureHash(getActivity(),
                    svcInfo.applicationInfo.packageName);
            String expected = getActivity().getString(R.string.expected_sig_hash);

            if (expected.equals(otherHash)) {
                Intent explicit = new Intent(implicit);
                ComponentName cn = new ComponentName(svcInfo.applicationInfo.packageName, svcInfo.name);

                explicit.setComponent(cn);
                appContext.bindService(explicit, this, Context.BIND_AUTO_CREATE);
            } else {
                Toast.makeText(getActivity(), "Unexpected signature found!", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Exception trying to get signature hash", e);
        }
    }
}

From source file:org.techbooster.gcmsample.GcmBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    // intentGcmIntentService???
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
    // ??WakeLock???
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);/*ww w.  ja v  a 2  s.c  o  m*/
}

From source file:com.devbrackets.android.playlistcore.helper.MediaControlsHelper.java

@NonNull
protected PendingIntent getMediaButtonReceiverPendingIntent(@NonNull ComponentName componentName,
        @NonNull Class<? extends Service> serviceClass) {
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(componentName);

    mediaButtonIntent.putExtra(RECEIVER_EXTRA_CLASS, serviceClass.getName());
    return PendingIntent.getBroadcast(context, 0, mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

public Notification createPaused() {
    Notification notification = new Notification();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notif_download);

    views.setProgressBar(R.id.progressDownload, 100, 0, false);
    views.setTextViewText(R.id.notifTitle, context.getResources().getString(R.string.paused));
    views.setTextViewText(R.id.notifArtist, track.getArtist() + "-" + track.getTitle());
    views.setImageViewResource(R.id.notifPause, R.drawable.widget_play);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(new ComponentName(context, DownloadingService.class));

    Intent intentPause = new Intent(PlayingService.actionIconPlay);
    intentPause.putExtra("aid", track.getAid());
    intentPause.setComponent(new ComponentName(context, DownloadingService.class));

    views.setOnClickPendingIntent(R.id.notifClose,
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    views.setOnClickPendingIntent(R.id.notifPause,
            PendingIntent.getService(context, notificationId, intentPause, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = views;//from  w  ww. j  a  va  2 s.com
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.download_icon;

    return notification;
}

From source file:github.daneren2005.dsub.util.Util.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing) {

    // Use the same text for the ticker and the expanded notification
    String title = song.getTitle();
    String arist = song.getArtist();
    String album = song.getAlbum();

    // Set the album art.
    try {//  w  ww  .  j  a va 2  s  . c om
        int size = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
        Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, size);
        if (bitmap == null) {
            // set default album art
            rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            rv.setImageViewBitmap(R.id.notification_image, bitmap);
        }
    } catch (Exception x) {
        Log.w(TAG, "Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    // set the text for the notifications
    rv.setTextViewText(R.id.notification_title, title);
    rv.setTextViewText(R.id.notification_artist, arist);
    rv.setTextViewText(R.id.notification_album, album);

    Pair<Integer, Integer> colors = getNotificationTextColors(context);
    if (colors.getFirst() != null) {
        rv.setTextColor(R.id.notification_title, colors.getFirst());
    }
    if (colors.getSecond() != null) {
        rv.setTextColor(R.id.notification_artist, colors.getSecond());
    }

    if (!playing) {
        rv.setImageViewResource(R.id.control_pause, R.drawable.notification_play);
        rv.setImageViewResource(R.id.control_previous, R.drawable.notification_stop);
    }

    // Create actions for media buttons
    PendingIntent pendingIntent;
    if (playing) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    } else {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    }

    Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
    pauseIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_pause, pendingIntent);

    Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
    nextIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    nextIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
    pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_next, pendingIntent);
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

private Notification createStartingNew() {
    Notification notification = new Notification();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notif_download);

    views.setProgressBar(R.id.progressDownload, 100, 0, true);
    views.setTextViewText(R.id.notifTitle, context.getResources().getString(R.string.Downloading));
    views.setTextViewText(R.id.notifArtist, track.getArtist() + "-" + track.getTitle());
    views.setImageViewResource(R.id.notifPause, R.drawable.widget_pause);

    ComponentName componentName = new ComponentName(context, DownloadingService.class);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(componentName);

    Intent intentPause = new Intent(PlayingService.actionIconPause);
    intentPause.putExtra("aid", track.getAid());
    intentPause.setComponent(componentName);

    views.setOnClickPendingIntent(R.id.notifClose,
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    views.setOnClickPendingIntent(R.id.notifPause,
            PendingIntent.getService(context, notificationId, intentPause, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = views;//from   w w  w  .j av a 2  s  .co  m
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.download_icon;

    return notification;

}

From source file:com.github.michalbednarski.intentslab.editor.ComponentPickerDialogFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);/* w  w  w.j  av a  2s  .  co  m*/

    // Get edited intent
    IntentEditorActivity intentEditor = (IntentEditorActivity) getActivity();
    Intent intent = new Intent(intentEditor.getEditedIntent());
    intent.setComponent(null);

    // Get components
    PackageManager pm = intentEditor.getPackageManager();
    List<ResolveInfo> ri = null;

    switch (intentEditor.getComponentType()) {
    case IntentEditorConstants.ACTIVITY:
        ri = pm.queryIntentActivities(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    case IntentEditorConstants.BROADCAST:
        ri = pm.queryBroadcastReceivers(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    case IntentEditorConstants.SERVICE:
        ri = pm.queryIntentServices(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    }

    // Cancel if no components
    if (ri.isEmpty()) {
        Toast.makeText(getActivity(), R.string.no_matching_components_found, Toast.LENGTH_SHORT).show();
        dismiss();
        return;
    }

    // Split enabled and disabled choices
    ArrayList<ResolveInfo> choices = new ArrayList<ResolveInfo>();
    ArrayList<ResolveInfo> disabledChoices = new ArrayList<ResolveInfo>();
    for (ResolveInfo resolveInfo : ri) {
        (isComponentEnabled(pm, resolveInfo) ? choices : disabledChoices).add(resolveInfo);
    }

    mEnabledChoicesCount = choices.size();
    choices.addAll(disabledChoices);
    mChoices = choices.toArray(new ResolveInfo[choices.size()]);
}

From source file:com.farmerbb.taskbar.activity.DashboardActivity.java

private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();/*from  ww  w .j a  v a2 s  .com*/
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);

        shouldFinish = false;
    } else {
        createWidget(data);
    }
}

From source file:com.farmerbb.taskbar.adapter.StartMenuAdapter.java

@Override
public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null)
        convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row,
                parent, false);//www .  j av a  2s. co  m

    final AppEntry entry = getItem(position);
    assert entry != null;

    final SharedPreferences pref = U.getSharedPreferences(getContext());

    TextView textView = (TextView) convertView.findViewById(R.id.name);
    textView.setText(entry.getLabel());

    Intent intent = new Intent();
    intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
    ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);

    if (activityInfo != null)
        textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);

    switch (pref.getString("theme", "light")) {
    case "light":
        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
        break;
    case "dark":
        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
        break;
    }

    ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
    imageView.setImageDrawable(entry.getIcon(getContext()));

    LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
    layout.setOnClickListener(view -> {
        LocalBroadcastManager.getInstance(getContext())
                .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
        U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(),
                entry.getUserId(getContext()), null, false, false);
    });

    layout.setOnLongClickListener(view -> {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        openContextMenu(entry, location);
        return true;
    });

    layout.setOnGenericMotionListener((view, motionEvent) -> {
        int action = motionEvent.getAction();

        if (action == MotionEvent.ACTION_BUTTON_PRESS
                && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            openContextMenu(entry, location);
        }

        if (action == MotionEvent.ACTION_SCROLL && pref.getBoolean("visual_feedback", true))
            view.setBackgroundColor(0);

        return false;
    });

    if (pref.getBoolean("visual_feedback", true)) {
        layout.setOnHoverListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
                int backgroundTint = pref.getBoolean("transparent_start_menu", false)
                        ? U.getAccentColor(getContext())
                        : U.getBackgroundTint(getContext());

                //noinspection ResourceAsColor
                backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
                v.setBackgroundColor(backgroundTint);
            }

            if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
                v.setBackgroundColor(0);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));

            return false;
        });

        layout.setOnTouchListener((v, event) -> {
            v.setAlpha(
                    event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE
                            ? 0.5f
                            : 1);
            return false;
        });
    }

    return convertView;
}

From source file:com.akalizakeza.apps.ishusho.activity.NewPostActivity.java

@AfterPermissionGranted(RC_CAMERA_PERMISSIONS)
private void showImagePicker() {
    // Check for camera permissions
    if (!EasyPermissions.hasPermissions(this, cameraPerms)) {
        EasyPermissions.requestPermissions(this, "This sample will upload a picture from your Camera",
                RC_CAMERA_PERMISSIONS, cameraPerms);
        return;/*w w w . ja v  a2 s  .  c  o m*/
    }

    // Choose file storage location
    File file = new File(getExternalCacheDir(), UUID.randomUUID().toString());
    mFileUri = Uri.fromFile(file);

    // Camera
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
        cameraIntents.add(intent);
    }

    // Image Picker
    Intent pickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    Intent chooserIntent = Intent.createChooser(pickerIntent, getString(R.string.picture_chooser_title));
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
    startActivityForResult(chooserIntent, TC_PICK_IMAGE);
}