android.content.Context#startForegroundService ( )源码实例Demo

下面列出了android.content.Context#startForegroundService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Focus   文件: AutoUpdateReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    //当前定时器已开启
    UserPreference.updateOrSaveValueByKey(UserPreference.tim_is_open,"1");

    String interval = UserPreference.queryValueByKey(UserPreference.tim_interval,"-1");
    if (!interval.equals("-1")){//这个地方判断是因为有可能在定时器运行期间,用户取消了定时器,这样
        Intent i = new Intent(context, TimingService.class);
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(i);
            } else {
                context.startService(i);
            }
        }catch (Exception e){
            UserPreference.updateOrSaveValueByKey(UserPreference.back_error,e.getMessage());
        }
    }else {
    }
}
 
源代码2 项目: libcommon   文件: ServiceRecorder.java
/**
 * コンストラクタ
 * @param context
 * @param callback
 */
@SuppressLint("NewApi")
public ServiceRecorder(@NonNull final Context context,
	@NonNull final Callback callback) {

	if (DEBUG) Log.v(TAG, "コンストラクタ:");
	mWeakContext = new WeakReference<>(context);
	mCallback = callback;

	final Intent serviceIntent = createServiceIntent(context);
	if (BuildCheck.isOreo()) {
		context.startForegroundService(serviceIntent);
	} else {
		context.startService(serviceIntent);
	}
	doBindService();
}
 
private static void startService(Context context, String command) {
    final Intent intent = new Intent(context, MusicService.class);
    intent.setAction(command);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        try {
            // IMPORTANT NOTE: (kind of a hack)
            // on Android O and above the following crashes when the app is not running
            // there is no good way to check whether the app is running so we catch the exception
            // we do not always want to use startForegroundService() because then one gets an ANR
            // if no notification is displayed via startForeground()
            // according to Play analytics this happens a lot, I suppose for example if command = PAUSE
            context.startService(intent);
        } catch (IllegalStateException ignored) {
            ContextCompat.startForegroundService(context, intent);
        }
    }
}
 
源代码4 项目: prayer-times-android   文件: AlarmService.java
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
    int alarmId = intent.getIntExtra(EXTRA_ALARMID, -1);
    long time = intent.getLongExtra(EXTRA_TIME, -1);
    if (alarmId > 0 && time > 0) {
        Intent service = new Intent(context, AlarmService.class);
        service.putExtra(EXTRA_ALARMID, alarmId);
        service.putExtra(EXTRA_TIME, time);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(service);
        } else {
            context.startService(service);
        }
    } else {
        Times.setAlarms();
    }
}
 
源代码5 项目: cim   文件: CIMPushManager.java
public static void startService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
源代码6 项目: mollyim-android   文件: WipeMemoryService.java
private static void startForegroundService(Context context) {
  Intent intent = new Intent(context, WipeMemoryService.class);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
  } else {
    context.startService(intent);
  }
}
 
源代码7 项目: SmartPack-Kernel-Manager   文件: Utils.java
public static void startService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
源代码8 项目: hyperion-android-grabber   文件: BootActivity.java
public static void startScreenRecorder(Context context, int resultCode, Intent data) {
    Intent intent = new Intent(context, HyperionScreenService.class);
    intent.setAction(HyperionScreenService.ACTION_START);
    intent.putExtra(HyperionScreenService.EXTRA_RESULT_CODE, resultCode);
    intent.putExtras(data);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
private static void startService(Context context, String command) {
    final Intent intent = new Intent(context, MusicService.class);
    intent.setAction(command);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
public static void startCalendarIntegration(@NonNull Context context) {
    Intent intent = new Intent(context, CalendarIntegrationService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
源代码11 项目: FileDownloader   文件: BaseFileServiceUIGuard.java
@Override
public void bindStartByContext(final Context context, final Runnable connectedRunnable) {
    if (FileDownloadUtils.isDownloaderProcess(context)) {
        throw new IllegalStateException("Fatal-Exception: You can't bind the "
                + "FileDownloadService in :filedownloader process.\n It's the invalid operation"
                + " and is likely to cause unexpected problems.\n Maybe you want to use"
                + " non-separate process mode for FileDownloader, More detail about "
                + "non-separate mode, please move to wiki manually:"
                + " https://github.com/lingochamp/FileDownloader/wiki/filedownloader.properties"
        );
    }

    if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(this, "bindStartByContext %s", context.getClass().getSimpleName());
    }

    Intent i = new Intent(context, serviceClass);
    if (connectedRunnable != null) {
        if (!connectedRunnableList.contains(connectedRunnable)) {
            connectedRunnableList.add(connectedRunnable);
        }
    }

    if (!bindContexts.contains(context)) {
        // 对称,只有一次remove,防止内存泄漏
        bindContexts.add(context);
    }

    runServiceForeground = FileDownloadUtils.needMakeServiceForeground(context);
    i.putExtra(ExtraKeys.IS_FOREGROUND, runServiceForeground);
    context.bindService(i, this, Context.BIND_AUTO_CREATE);
    if (runServiceForeground) {
        if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(i);
    } else {
        context.startService(i);
    }
}
 
源代码12 项目: AudioAnchor   文件: MediaButtonIntentReceiver.java
private static void startService(Context context, String command) {
    final Intent intent = new Intent(context, MediaPlayerService.class);
    intent.setAction(command);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
源代码13 项目: hyperion-android-grabber   文件: ToggleActivity.java
private static void startScreenRecorder(Context context, int resultCode, Intent data) {
    Intent intent = new Intent(context, HyperionScreenService.class);
    intent.setAction(HyperionScreenService.ACTION_START);
    intent.putExtra(HyperionScreenService.EXTRA_RESULT_CODE, resultCode);
    intent.putExtras(data);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
源代码14 项目: Taskbar   文件: U.java
public static void startForegroundService(Context context, Intent intent) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if(Settings.canDrawOverlays(context))
            context.startForegroundService(intent);
    } else
        context.startService(intent);
}
 
源代码15 项目: InviZible   文件: ModulesAux.java
private static void sendIntent(Context context, String action) {
    Intent intent = new Intent(context, ModulesService.class);
    intent.setAction(action);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        intent.putExtra("showNotification", true);
        context.startForegroundService(intent);
    } else {
        intent.putExtra("showNotification", isShowNotification(context));
        context.startService(intent);
    }
}
 
@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received intent:" + intent);
    // Long running tasks, like calling Account Transfer API, shouldn't happen here. Start a
    // foreground service to perform long running tasks.
    Intent serviceIntent = AccountTransferService.getIntent(context, intent.getAction());
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(serviceIntent);
    } else {
        context.startService(serviceIntent);
    }
}
 
源代码17 项目: SSLSocks   文件: StunnelIntentService.java
/**
 * Starts this service to perform action Foo with the given parameters. If
 * the service is already performing a task this action will be queued.
 *
 * @see IntentService
 */
public static void start(Context context) {
	Intent intent = new Intent(context, StunnelIntentService.class);
	intent.setAction(ACTION_STARTNOVPN);
	if (android.os.Build.VERSION.SDK_INT >= 26) {
		context.startForegroundService(intent);
	} else {
		context.startService(intent);
	}
}
 
源代码18 项目: OpenFit   文件: StartOpenFitAtBootReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent serviceIntent = new Intent(context, OpenFitService.class);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(serviceIntent);
        } else {
            context.startService(serviceIntent);
        }
    }
}
 
源代码19 项目: island   文件: IslandProvisioning.java
public static void start(final Context context, final @Nullable String action) {
	final Intent intent = new Intent(action).setComponent(new ComponentName(context, IslandProvisioning.class));
	if (SDK_INT >= O) context.startForegroundService(intent);
	else context.startService(intent);
}
 
源代码20 项目: deagle   文件: Contexts.java
/**
 * startForegroundService() was introduced in O, just call startService() before O.
 *
 * @param context Context to start Service from.
 * @param intent The description of the Service to start.
 *
 * @see Context#startForegroundService(Intent)
 */
public static ComponentName startForegroundService(final Context context, final Intent intent) {
	return SDK_INT >= O ? context.startForegroundService(intent) : context.startService(intent);
}
 
 方法所在类
 同类方法