Example usage for android.content Context startService

List of usage examples for android.content Context startService

Introduction

In this page you can find the example usage for android.content Context startService.

Prototype

@Nullable
public abstract ComponentName startService(Intent service);

Source Link

Document

Request that a given application service be started.

Usage

From source file:com.hang.exoplayer.PlayService.java

public static void loadMedia(Context packageContext, List<String> playingAddress, int position) {
    boolean isNativeAudio = (playingAddress != null && playingAddress.size() > 0)
            ? playingAddress.get(0).startsWith("file:")
            : false;// w  ww. j  a  va2s .c o  m
    Intent intent = new Intent(packageContext, PlayService.class);
    intent.putExtra(PlayService.ACTION_LOAD, true);
    ArrayList<String> playingAddresses = new ArrayList<>(playingAddress);
    intent.putExtra(PlayService.KEY_LIST, playingAddresses);
    intent.putExtra(PlayService.KEY_POS, position);
    if (isNativeAudio || com.hang.exoplayer.bean.Util.canPlay()) {
        packageContext.startService(intent);
    } else if (com.hang.exoplayer.bean.Util.getNetType() > -1) {
        Bundle bundle = intent.getExtras();
        if (packageContext instanceof FragmentActivity) {
            NetInfoFragment netInfoFragment = NetInfoFragment.newInstance(bundle);
            netInfoFragment.show(((FragmentActivity) packageContext).getSupportFragmentManager(), "netInfo");
        } else {
            throw new RuntimeException("loadMedia Activity must be FragmentActivity");
        }
    } else {
        Toast.makeText(ExoApplication.getApplication(), "???~",
                Toast.LENGTH_LONG).show();
    }

}

From source file:de.azapps.mirakel.services.NotificationService.java

/**
 * Update the MirakelNotifications, Reminders and the widgets
 *
 * @param context// ww w  .  j ava2 s  .c  o  m
 */
public static void updateServices(final Context context) {
    // Widget update
    final Intent widgetIntent;
    try {
        widgetIntent = new Intent(context, Class.forName(DefinitionsHelper.MAINWIDGET_CLASS));
    } catch (final ClassNotFoundException e) {
        Log.wtf(TAG, "widget not found", e);
        return;
    }
    widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, DefinitionsHelper.widgets);
    context.sendBroadcast(widgetIntent);
    // Dashclock update
    final Intent dashclockIntent = new Intent();
    dashclockIntent.setAction("de.azapps.mirakel.dashclock.UPDATE");
    context.sendBroadcast(dashclockIntent);
    if (NotificationService.notificationService == null) {
        final Intent intent = new Intent(context, NotificationService.class);
        context.startService(intent);
    } else {
        NotificationService.notificationService.notifier();
    }
}

From source file:syncthing.android.service.SyncthingUtils.java

public static void importConfig(Context context, Uri uri, boolean force) {
    File configDir = getConfigDirectory(context);
    if (configDir.exists()) {
        if (!force) {
            new AlertDialog.Builder(context).setTitle(R.string.overwrite)
                    .setMessage(R.string.overwrite_current_config)
                    .setNegativeButton(android.R.string.cancel, null)
                    .setPositiveButton(android.R.string.ok, (dialog, which) -> importConfig(context, uri, true))
                    .show();/*from  www.j  a v  a  2  s  . c o  m*/
            return;
        } else {
            context.startService(
                    new Intent(context, SyncthingInstance.class).setAction(SyncthingInstance.SHUTDOWN));
            try {
                FileUtils.cleanDirectory(configDir);
            } catch (IOException e) {
                Toast.makeText(context, R.string.error, Toast.LENGTH_LONG).show();
                Timber.e("Failed to import", e);
                return;
            }
        }
    }
    InputStream is = null;
    try {
        //TODO copy zip to temp location and check if its a valid config
        is = context.getContentResolver().openInputStream(uri);
        ZipUtil.unpack(is, configDir);
        File[] files = configDir.listFiles();
        for (File f : files) {
            Runtime.getRuntime().exec("chmod 0600 " + f.getAbsolutePath()).waitFor();
            Timber.d("chmod 0600 on %s", f.getAbsolutePath());
        }
        Toast.makeText(context, R.string.config_imported, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        FileUtils.deleteQuietly(configDir);
        Toast.makeText(context, R.string.error, Toast.LENGTH_LONG).show();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.chromium.chrome.browser.media.MediaCaptureNotificationService.java

/**
 * Send an intent to MediaCaptureNotificationService to either create, update or destroy the
 * notification identified by tabId.//from ww w  .  ja  v  a  2 s  . c  om
 * @param tabId Unique notification id.
 * @param mediaType The media type that is being captured.
 * @param fullUrl Url of the current webrtc call.
 */
public static void updateMediaNotificationForTab(Context context, int tabId, int mediaType, String fullUrl) {
    if (!shouldStartService(context, mediaType, tabId))
        return;
    Intent intent = new Intent(context, MediaCaptureNotificationService.class);
    intent.setAction(ACTION_MEDIA_CAPTURE_UPDATE);
    intent.putExtra(NOTIFICATION_ID_EXTRA, tabId);
    String baseUrl = fullUrl;
    try {
        URL url = new URL(fullUrl);
        baseUrl = url.getProtocol() + "://" + url.getHost();
    } catch (MalformedURLException e) {
        Log.w(TAG, "Error parsing the webrtc url, %s ", fullUrl);
    }
    intent.putExtra(NOTIFICATION_MEDIA_URL_EXTRA, baseUrl);
    intent.putExtra(NOTIFICATION_MEDIA_TYPE_EXTRA, mediaType);
    context.startService(intent);
}

From source file:com.upnext.blekit.BLEKit.java

private static void restartBlekit(Context context) {
    Intent intnt = new Intent("com.upnext.blekit.service.RESTARTER");
    intnt.setPackage(context.getPackageName());
    if (context.getPackageManager().resolveService(intnt, 0) != null) {
        context.startService(intnt);
    }//from  ww  w .j  a  v a  2s.c o  m
}

From source file:nu.yona.app.utils.AppUtils.java

/**
 * Start service once user grant permission for application permission (for 5.1+ version)
 *
 * @param context the context//w ww  .j  a  va 2  s  . co m
 */
public static void startService(Context context) {
    try {
        activityMonitorIntent = new Intent(context, ActivityMonitorService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(context, activityMonitorIntent);
            cancelPendingWakeUpAlarms(context);
        } else {
            context.startService(activityMonitorIntent);
        }
    } catch (Exception e) {
        reportException(AppUtils.class.getSimpleName(), e, Thread.currentThread());
    }
}

From source file:id.ridon.keude.UpdateService.java

public static UpdateReceiver updateRepoNow(String address, Context context) {
    Intent intent = new Intent(context, UpdateService.class);
    UpdateReceiver receiver = new UpdateReceiver(new Handler());
    receiver.showDialog(context);//from  www .  j  a v a  2s. com
    intent.putExtra(EXTRA_RECEIVER, receiver);
    if (!TextUtils.isEmpty(address)) {
        intent.putExtra(EXTRA_ADDRESS, address);
    }
    context.startService(intent);

    return receiver;
}

From source file:org.chromium.chrome.browser.media.MediaNotificationService.java

/**
 * Send an intent to MediaNotificationService to either create, update or destroy the
 * notification identified by tabId.// w ww  .  j  av  a 2s  . c o m
 * @param tabId Unique notification id.
 * @param audio If audio is being captured.
 * @param video If video is being captured.
 * @param fullUrl Url of the current webrtc call.
 */
public static void updateMediaNotificationForTab(Context context, int tabId, boolean audioCapture,
        boolean videoCapture, boolean audioPlayback, String fullUrl) {
    int mediaType = getMediaType(audioCapture, videoCapture, audioPlayback);
    if (!shouldStartService(context, mediaType, tabId))
        return;
    Intent intent = new Intent(context, MediaNotificationService.class);
    intent.putExtra(NOTIFICATION_ID_EXTRA, tabId);
    String baseUrl = fullUrl;
    try {
        URL url = new URL(fullUrl);
        baseUrl = url.getProtocol() + "://" + url.getHost();
    } catch (MalformedURLException e) {
        Log.w(LOG_TAG, "Error parsing the webrtc url " + fullUrl);
    }
    intent.putExtra(NOTIFICATION_MEDIA_URL_EXTRA, baseUrl);
    intent.putExtra(NOTIFICATION_MEDIA_TYPE_EXTRA, mediaType);
    context.startService(intent);
}

From source file:com.github.michalbednarski.intentslab.ReceiveBroadcastService.java

public static void startReceiving(Context context, IntentFilter[] filters, boolean multiple) {
    if (!multiple) {
        for (IntentFilter filter : filters) {
            Intent stickyBroadcastIntent = context.registerReceiver(null, filter);
            if (stickyBroadcastIntent != null) {
                context.startActivity(new Intent(context, IntentEditorActivity.class)
                        .putExtra(IntentEditorActivity.EXTRA_INTENT, stickyBroadcastIntent)
                        .putExtra(IntentEditorActivity.EXTRA_COMPONENT_TYPE, IntentEditorConstants.BROADCAST));
                Toast.makeText(context, R.string.received_sticky_broadcast, Toast.LENGTH_SHORT).show();
                return;
            }/*from w w w.jav a 2 s  . c  o  m*/
        }
    }

    context.startService(new Intent(context, ReceiveBroadcastService.class).putExtra("intentFilters", filters)
            .putExtra("multiple", multiple));
}

From source file:com.tonyodev.fetch.FetchService.java

public static void sendToService(@NonNull Context context, @Nullable Bundle extras) {

    if (context == null) {
        throw new NullPointerException("Context cannot be null");
    }//  w  w w . j  a  va  2s . co  m

    if (extras == null) {
        extras = new Bundle();
    }

    Intent intent = new Intent(context, FetchService.class);
    intent.putExtras(extras);
    context.startService(intent);
}