下面列出了android.support.v4.content.ContextCompat#startForegroundService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
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);
}
}
}
}
@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);
}
}
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();
}
}
}
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");
}
}
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();
}
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);
}
public static void PlayVideo(String[] ytUrls) {
Intent intent = new Intent(activity, MusicService.class);
intent.setAction("PlayVideo");
intent.putExtra("urls", ytUrls);
ContextCompat.startForegroundService(activity, intent);
}
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);
}
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);
}
}
/**
* 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);
}
@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));
}
}
@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);
}
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);
}
private void createBackup() {
ContextCompat.startForegroundService(this, new Intent(this, ExportBackupService.class));
}
public static void start(Context ctx) {
Intent startIntent = new Intent(ctx, MainService.class);
startIntent.setAction(START_FOREGROUND_ACTION);
ContextCompat.startForegroundService(ctx, startIntent);
}
@Override
public void sync() {
Intent trackerService = new Intent(mContext, TrackerService.class);
trackerService.setAction(ACTION_SYNC);
ContextCompat.startForegroundService(mContext, trackerService);
}
public static void ChangeVideoOffline(int position) {
Intent intent = new Intent(activity, MusicService.class);
intent.setAction("ChangeVideoOffline");
intent.putExtra("pos", position);
ContextCompat.startForegroundService(activity, intent);
}
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);
}
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);
}