Java Code Examples for android.support.v4.content.ContextCompat#startForegroundService()

The following examples show how to use android.support.v4.content.ContextCompat#startForegroundService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiPushPingReceiver.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
public void onReceive(Context paramContext, Intent paramIntent) {
    Alarm.registerPing(false);
    MyLog.v(paramIntent.getPackage() + " is the package name");
    if (PushConstants.ACTION_PING_TIMER.equals(paramIntent.getAction())) {
        if (TextUtils.equals(paramContext.getPackageName(), paramIntent.getPackage())) {
            MyLog.v("Ping XMChannelService on timer");

            try {
                Intent localIntent = new Intent(paramContext, PushServiceMain.class);
                localIntent.putExtra(PushServiceConstants.EXTRA_TIME_STAMP, System.currentTimeMillis());
                localIntent.setAction(PushServiceConstants.ACTION_TIMER);
                ContextCompat.startForegroundService(paramContext, localIntent);
            } catch (Exception localException) {
                MyLog.e(localException);
            }

        } else {
            MyLog.w("cancel the old ping timer");
            Alarm.stop();
        }
    }
}
 
Example 2
Source File: KeepAliveReceiver.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    try {
        long now = System.currentTimeMillis();

        if ((now - lastActive) < (1000 * 60 * 2)) {
            return;
        }

        lastActive = now;

        logger.d("start service when " + intent.getAction());
        Intent localIntent = new Intent(context, PushServiceMain.class);
        localIntent.putExtra(PushServiceConstants.EXTRA_TIME_STAMP, now);
        localIntent.setAction(PushServiceConstants.ACTION_CHECK_ALIVE);
        ContextCompat.startForegroundService(context, localIntent);
    } catch (Exception localException) {
        MyLog.e(localException);
    }
}
 
Example 3
Source File: PkgUninstallReceiver.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
public void onReceive(Context var1, Intent var2) {
    if (var2 != null && var2.getExtras() !=null&& "android.intent.action.PACKAGE_REMOVED".equals(var2.getAction())) {
        boolean var3 = var2.getExtras().getBoolean("android.intent.extra.REPLACING");
        Uri var4 = var2.getData();
        if (var4 != null && !var3) {
            try {
                Intent var5 = new Intent(var1, PushServiceMain.class);
                var5.setAction(PushServiceConstants.ACTION_UNINSTALL);
                var5.putExtra(PushServiceConstants.EXTRA_UNINSTALL_PKG_NAME, var4.getEncodedSchemeSpecificPart());
                ContextCompat.startForegroundService(var1, var5);
                GeoFenceUtils.appIsUninstalled(var1.getApplicationContext(), var4.getEncodedSchemeSpecificPart());
            } catch (Exception var7) {
                MyLog.e(var7);
            }
        }
    }

}
 
Example 4
Source File: SightRemote.java    From SightRemote with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    instance = this;
    Fabric.with(this, new Crashlytics());

    PreferenceManager.setDefaultValues(this, R.xml.settings, true);

    NotificationCenter.setupUpChannels();
    serviceConnector = new SightServiceConnector(this);
    serviceConnector.connectToService();

    ContextCompat.startForegroundService(this, new Intent(this, OngoingNotificationService.class));
    startService(new Intent(this, SightService.class));
    startService(new Intent(this, HistorySyncService.class));
    startService(new Intent(this, AlertService.class));
    if (Preferences.getBooleanPref(Preferences.PREF_BOOLEAN_AUTO_ADJUST_TIME))
        startService(new Intent(this, TimeSynchronizationService.class));
}
 
Example 5
Source File: BootLoader.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void checkTrackerService(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    boolean restoreTrack = preferences.getBoolean(SettingsConstants.KEY_PREF_TRACK_RESTORE, false);

    if (TrackerService.hasUnfinishedTracks(context)) {
        Intent trackerService = new Intent(context, TrackerService.class);
        if (!restoreTrack)
            trackerService.setAction(TrackerService.ACTION_STOP);

        ContextCompat.startForegroundService(context, trackerService);
    }
}
 
Example 6
Source File: IpcService.java    From OneTapVideoDownload with GNU General Public License v3.0 5 votes vote down vote up
public static void startSaveUrlAction(Context context, String uri, String packageName) {
    Intent intent = new Intent(ACTION_SAVE_BROWSER_VIDEO);
    intent.setClassName(PACKAGE_NAME, CLASS_NAME);
    intent.putExtra(EXTRA_URL, uri);
    intent.putExtra(EXTRA_PACKAGE_NAME, packageName);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 7
Source File: MonitoringFragment.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void startMonitoringService(MonitoringSettings ms) {
    // Start service
    Intent intent = new Intent(getActivity(), MonitoringService.class);
    intent.setAction(MonitoringService.START_ACTION);
    intent.putExtra(MonitoringService.ARGUMENT_MON_SETTINGS, ms);
    ContextCompat.startForegroundService(getContext(), intent);
}
 
Example 8
Source File: BootReceiver.java    From KA27 with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if (Intent.ACTION_BOOT_COMPLETED.equals(action) && Utils.getBoolean("ka_run", false, context)) {
        ContextCompat.startForegroundService(context, new Intent(context, BootService.class));

        if (Utils.getBoolean("emulateinit.d", false, context))
            ContextCompat.startForegroundService(context, new Intent(context, InitdService.class));
    }
}
 
Example 9
Source File: NetBare.java    From NetBare with MIT License 5 votes vote down vote up
/**
 * Start the NetBare service with your specific configuration. If the service is started,
 * {@link NetBareListener#onServiceStarted()} will be invoked.
 *
 * @param config The configuration for NetBare service.
 */
public void start(@NonNull NetBareConfig config) {
    if (config.mtu <= 0) {
        throw new RuntimeException("Must set mtu in NetBareConfig");
    }
    if (config.address == null) {
        throw new RuntimeException("Must set address in NetBareConfig");
    }
    mNetBareConfig = config;
    Intent intent = new Intent(NetBareService.ACTION_START);
    intent.setPackage(mApp.getPackageName());
    ContextCompat.startForegroundService(mApp, intent);
}
 
Example 10
Source File: MainActivity.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static void PlayVideo(String[] ytUrls, int position) {
    Intent intent = new Intent(activity, MusicService.class);
    intent.setAction("PlayVideo_pos");
    intent.putExtra("urls", ytUrls);
    intent.putExtra("pos", position);
    ContextCompat.startForegroundService(activity, intent);
}
 
Example 11
Source File: MainActivity.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static void PlayVideo(String[] ytUrls) {

        Intent intent = new Intent(activity, MusicService.class);
        intent.setAction("PlayVideo");
        intent.putExtra("urls", ytUrls);
        ContextCompat.startForegroundService(activity, intent);
    }
 
Example 12
Source File: MainActivity.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static void PlayVideo_Local(Context context, String[] urls, int position) {
    /** YTUrls here will work as path to music file...
     *
     *  Background task will load all the details about music
     *  and will set it to player and respective fields.
     */

    Intent intent = new Intent(context, MusicService.class);
    intent.setAction("PlayVideo_Local_pos");
    intent.putExtra("urls", urls);
    intent.putExtra("pos", position);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 13
Source File: SilentDownloadActivity.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public void okClick(View view) {
    String ext = "mp3";
    if (mRadioM4a.isChecked()) ext = "m4a";
    else if (mRadio1080p.isChecked()) ext = "1080p";
    else if (mRadio720p.isChecked()) ext = "720p";
    else if (mRadio480p.isChecked()) ext = "480p";

    if (meta == null) {
        Toast.makeText(this, "Error: Parsing video!", Toast.LENGTH_SHORT).show();
        finish();
    }

    String title = meta.getTitle();
    String author = meta.getAuthor();

    if (title.contains("-") && !title.contains("auto-generate")) {
        title = meta.getTitle().split("-")[1];
        author = meta.getTitle().split("-")[0];
    }

    YTConfig config = new YTConfig("auto-generate", "auto-generate", ext
            , title, author, true, meta.getImgUrl());
    config.setVideoID(meta.getVideoID());

    Log.e(TAG, "okClick: Download Name: " + YTutils.getTargetName(config));

    config.setTargetName(YTutils.getTargetName(config));
    config.setTaskExtra("autoTask");

    if (showAds && ad.isLoaded())
        ad.show();

    Intent serviceIntent = new Intent(this, IntentDownloadService.class);
    serviceIntent.putExtra("addJob", config);
    ContextCompat.startForegroundService(this, serviceIntent);
    finish();
}
 
Example 14
Source File: Compatibility.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static void startService(Context context, Intent intent) {
    try {
        if (Compatibility.runsAndTargetsTwentySix(context)) {
            intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
            ContextCompat.startForegroundService(context, intent);
        } else {
            context.startService(intent);
        }
    } catch (RuntimeException e) {
        Log.d(Config.LOGTAG, context.getClass().getSimpleName() + " was unable to start service");
    }
}
 
Example 15
Source File: IpcService.java    From OneTapVideoDownload with GNU General Public License v3.0 4 votes vote down vote up
public static void startSaveYoutubeVideoAction(Context context, String paramString) {
    Intent intent = new Intent(ACTION_SAVE_YOUTUBE_VIDEO);
    intent.setClassName(PACKAGE_NAME, CLASS_NAME);
    intent.putExtra(EXTRA_PARAM_STRING, paramString);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 16
Source File: SettingsActivity.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
private void createBackup() {
	ContextCompat.startForegroundService(this, new Intent(this, ExportBackupService.class));
}
 
Example 17
Source File: MainActivity.java    From YTPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static void ChangeVideoOffline(int position) {
    Intent intent = new Intent(activity, MusicService.class);
    intent.setAction("ChangeVideoOffline");
    intent.putExtra("pos", position);
    ContextCompat.startForegroundService(activity, intent);
}
 
Example 18
Source File: TrackLayerUI.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void sync() {
    Intent trackerService = new Intent(mContext, TrackerService.class);
    trackerService.setAction(ACTION_SYNC);
    ContextCompat.startForegroundService(mContext, trackerService);
}
 
Example 19
Source File: EditLayerOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void startGeometryByWalk() {
    // register broadcast events
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WalkEditService.WALKEDIT_CHANGE);
    mReceiver = new WalkEditReceiver();
    mContext.registerReceiver(mReceiver, intentFilter);
    mHasEdits = true;

    if (WalkEditService.isServiceRunning(mContext))
        return;

    // start service if not started yet
    GeoGeometry geometry = mFeature.getGeometry();
    int selectedRing = mSelectedItem.getSelectedRingId();
    int selectedGeometry = mDrawItems.indexOf(mSelectedItem);

    switch (mLayer.getGeometryType()) {
        case GeoConstants.GTLineString:
            break;
        case GeoConstants.GTPolygon:
            GeoPolygon polygon = ((GeoPolygon) geometry);
            geometry = selectedRing == 0 ? polygon.getOuterRing() : polygon.getInnerRing(selectedRing - 1);
            break;
        case GeoConstants.GTMultiLineString:
            geometry = ((GeoMultiLineString) geometry).get(selectedGeometry);
            break;
        case GeoConstants.GTMultiPolygon:
            GeoPolygon selectedPolygon = ((GeoMultiPolygon) geometry).get(selectedGeometry);
            geometry = selectedRing == 0 ? selectedPolygon.getOuterRing() : selectedPolygon.getInnerRing(selectedRing - 1);
            break;
        default:
            return;
    }

    Intent trackerService = new Intent(mContext, WalkEditService.class);
    trackerService.setAction(WalkEditService.ACTION_START);
    trackerService.putExtra(ConstantsUI.KEY_LAYER_ID, mLayer.getId());
    trackerService.putExtra(ConstantsUI.KEY_GEOMETRY, geometry);
    trackerService.putExtra(ConstantsUI.TARGET_CLASS, mContext.getClass().getName());
    ContextCompat.startForegroundService(mContext, trackerService);
}
 
Example 20
Source File: MainService.java    From meshenger-android with GNU General Public License v3.0 4 votes vote down vote up
public static void start(Context ctx) {
    Intent startIntent = new Intent(ctx, MainService.class);
    startIntent.setAction(START_FOREGROUND_ACTION);
    ContextCompat.startForegroundService(ctx, startIntent);
}