START_STICKY源码实例Demo

android.app.Service#START_STICKY源码实例Demo

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

@Override public int onStartCommand(Intent intent, int flags, int startId) {
    if (null == intent || null == intent.getAction()) {
        return Service.START_STICKY;
    }
    String action = intent.getAction();
    if (action.equals(ACTION_RESPONSE)) {
        Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
        CharSequence replyMessage = "";
        if (remoteInputResults != null) {
            replyMessage = remoteInputResults.getCharSequence(EXTRA_VOICE_REPLY);
        }
        String subject = intent.getStringExtra(Constants.PATH_KEY_MESSAGE_SUBJECT);
        String toUser = intent.getStringExtra(Constants.PATH_KEY_MESSAGE_TO_USER);
        String fullname = intent.getStringExtra(Constants.PATH_KEY_POST_FULLNAME);
        boolean isDirectMessage = intent.getBooleanExtra(Constants.PATH_KEY_IS_DIRECT_MESSAGE,
                false);
        sendReplyToPhone(replyMessage.toString(), fullname, toUser, subject, isDirectMessage);
    }
    return Service.START_STICKY;
}
 
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Start command");
		
/*
		Calendar calendar = Calendar.getInstance();
		calendar.set(2017, Calendar.APRIL, 1);
		if(System.currentTimeMillis() > calendar.getTime().getTime()) {
			return Service.START_STICKY;
		}
*/
        if (!started) { // the client app might autostart the service, but it might already be running
            started = true;

            startDetectingReader();
        }

        return Service.START_STICKY;
    }
 
源代码3 项目: open-quartz   文件: LocationService.java
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mLiveCard == null) {
            Log.d(LocationService.TAG, "Publishing LiveCard");
//            mLiveCard = mTimelineManager
//                    .createLiveCard(LocationService.LIVE_CARD_TAG);

            // Keep track of the callback to remove it before unpublishing.
            mCallback = new ChronometerDrawer(this);
            mLiveCard.setDirectRenderingEnabled(true).getSurfaceHolder()
                    .addCallback(mCallback);

            final Intent menuIntent = new Intent(this, MenuActivity.class);
            menuIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            mLiveCard.setAction(PendingIntent.getActivity(this, 0,
                    menuIntent, 0));

            mLiveCard.publish(PublishMode.REVEAL);
            Log.d(LocationService.TAG, "Done publishing LiveCard");
        } else {
            // TODO(alainv): Jump to the LiveCard when API is available.
        }

        return Service.START_STICKY;
    }
 
源代码4 项目: iGap-Android   文件: ServiceContact.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if ((ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)) {
        if (contentObserver == null) {
            contentObserver = new MyContentObserver();
            getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentObserver);
        }
    }

    SharedPreferences preferences = getApplicationContext().getSharedPreferences(SHP_SETTING.FILE_NAME, MODE_PRIVATE);
    if (preferences.getInt(SHP_SETTING.KEY_STNS_KEEP_ALIVE_SERVICE, 1) == 1) {
        return Service.START_STICKY;
    }
    return Service.START_NOT_STICKY;
}
 
源代码5 项目: retroband   文件: RetroBandService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	Log.d(TAG, "# Service - onStartCommand() starts here");
	
	// If service returns START_STICKY, android restarts service automatically after forced close.
	// At this time, onStartCommand() method in service must handle null intent.
	return Service.START_STICKY;
}
 
源代码6 项目: BTChat   文件: BTCTemplateService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	Logs.d(TAG, "# Service - onStartCommand() starts here");
	
	// If service returns START_STICKY, android restarts service automatically after forced close.
	// At this time, onStartCommand() method in service must handle null intent.
	return Service.START_STICKY;
}
 
源代码7 项目: PHONK   文件: PhonkServerService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MLog.d(TAG, "onStartCommand");

    if (intent != null) {
        AndroidUtils.debugIntent(TAG, intent);
        if (intent.getAction() == SERVICE_CLOSE) {
            EventBus.getDefault().postSticky(new Events.AppUiEvent("stopServers", ""));
            stopSelf();
        }
    }

    return Service.START_STICKY;
}
 
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    if(!created) {
        created = true;

        // Start registered collectors
        scm.registerSensorCollectors();
    }

    return Service.START_STICKY;
}
 
源代码9 项目: Popeens-DSub   文件: HeadphoneListenerService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	if(!Util.shouldStartOnHeadphones(this)) {
		stopSelf();
	}

	return Service.START_STICKY;
}
 
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	return Service.START_STICKY;
}
 
源代码11 项目: SmackAndroidDemo   文件: SmackService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    start();
    return Service.START_STICKY;
}
 
源代码12 项目: knopflerfish.org   文件: Knopflerfish.java
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
  // intent == null if restarted by the system -> start without init
  final String action = intent != null ? intent.getAction() : null;
  new Thread(new Runnable() {
    public void run() {
      synchronized (fwLock) {
        if (fw == null) {
          sendMessage(Msg.STARTING);
          try {
            fw = KfApk.newFramework(getStorage(),
                                    ACTION_INIT.equals(action),
                                    Knopflerfish.this.getAssets());
            if (fw != null) {
               sendMessage(Msg.STARTED,
                           (Serializable) KfApk.getFrameworkProperties());               
            } else {
               // framework did not init/start
               sendMessage(Msg.NOT_STARTED);
               stopSelf();
               return;
            }
           } catch (IOException ioe) {
             sendMessage(Msg.NOT_STARTED);
             Log.e(getClass().getName(), "Error starting framework", ioe);          
             stopSelf();
             return;
          }
        } else {
          // no op start
           sendMessage(Msg.NOP,
                       (Serializable) KfApk.getFrameworkProperties());               
          return;
        }
      }

      Framework fw_ = fw;
      if (fw_ != null) {
        KfApk.waitForStop(fw_); // does not return until shutdown
        stopSelf(); // shut down (then stop service) or stop():ed
      }
    }
  },"KF starter-waiter").start();

  return Service.START_STICKY; 
}
 
源代码13 项目: android_emulator_hacks   文件: Badservice.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}
 
源代码14 项目: QPM   文件: QPMPluginService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return Service.START_STICKY;
}
 
源代码15 项目: android-play-games-in-motion   文件: MainService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // The service is starting, due to a call to startService()
    Utils.logDebug(TAG, "onStartCommand");
    return Service.START_STICKY;
}
 
源代码16 项目: GPT   文件: ServiceProxy.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (DEBUG) {
        Log.d(TAG, "onStartCommand(Intent intent, int flags, int startId) : intent="
                + ((intent == null) ? "null." : intent.toString())
                + "; flags=" + flags + "; startId=" + startId);
    }

    if (mIActivityManagerProxy == null) {
        // 容错,不能支持Service了
        return 0;
    }

    if (intent == null) {
        return START_STICKY;
    }

    // 调用super,返回值用super的
    int ret = super.onStartCommand(intent, flags, startId);

    // 调用插件的onStartCommand方法
    int targetRet = 0;
    ComponentName target = getTargetComponent(intent);
    if (target == null) {
        if (mServices.isEmpty()) {
            stopSelf();
        }
        return ret;
    }

    // 插件SDK不能支持百度PushService,暂时先屏蔽了。
    if (TextUtils.equals(target.getClassName(), "com.baidu.android.pushservice.PushService")) {
        return ret;
    }

    // 获取SR
    ServiceRecord sr = mServices.get(target.toString());
    if (sr == null) {
        sr = loadTarget(intent, target, false);
    }

    // SR还是空的,可能是load失败了
    if (sr == null) {
        if (mServices.isEmpty()) {
            stopSelf();
        }
        return ret;
    }

    // 解决andorid 5.0 service get Serializable extra 找不到class的问题。
    intent.setExtrasClassLoader(ProxyEnvironment.getInstance(target.getPackageName()).getDexClassLoader());

    targetRet = sr.service.onStartCommand(intent, flags, startId);

    // 处理插件返回的ret
    switch (targetRet) {
        case Service.START_STICKY_COMPATIBILITY:
        case Service.START_STICKY: {
            sr.stopIfKilled = false;
            break;
        }
        case Service.START_NOT_STICKY: {
            if (sr.lastStartId == startId) {
                sr.stopIfKilled = true;
            }
            break;
        }
        case Service.START_REDELIVER_INTENT: {
            sr.lastIntent = new Intent(intent);
            sr.stopIfKilled = false;

            // 更新Intent
            updateServicesToSp();
            break;
        }
        default:
            throw new IllegalArgumentException("Unknown service start result: " + targetRet);
    }

    updateServicesToSp();

    return ret;
}
 
源代码17 项目: NewFastFrame   文件: KeepLiveService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}
 
源代码18 项目: Saiy-PS   文件: SelfAware.java
/**
 * We check here to see if the Text to Speech Engine should be warmed up, to avoid any future
 * initialisation delays. A 'race condition' of attempting to instantiate multiple TTS objects
 * is avoided by using the {@link #initSaiyTTS()} and following {@link #initTTS()} helper methods.
 */
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (!UtilsBundle.isSuspicious(intent)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onStartCommand: " + startId);
            MyLog.d(CLS_NAME, "onStartCommand: package: " + conditions.getPackage(intent));
        }

        if (conditions.shouldWarmUp(tts, intent)) {
            initTTS();
            restartStatusMonitor();
        }

        new InitStrings(getApplicationContext()).init();

        switch (conditions.checkNotificationInstruction(intent)) {

            case Condition.CONDITION_SELF_AWARE:
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "onStartCommand: CONDITION_SELF_AWARE");
                }
                startForeground(NotificationHelper.NOTIFICATION_SELF_AWARE);
                break;
            case Condition.CONDITION_NONE:
            default:
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "onStartCommand: CONDITION_NONE");
                }
                break;
        }

    } else {
        if (DEBUG) {
            MyLog.e(CLS_NAME, "onStartCommand: intent suspicious: stopping: " + startId);
        }

        this.stopSelf(startId);
    }

    return Service.START_STICKY;

}
 
源代码19 项目: biermacht   文件: BrewTimerService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  if (intent == null) {
    Log.e("BrewTimerService", "NULL intent passed to brew timer service.  Likely due to " +
            "a service restart.  Canceling the service!");
    stopSelf();
    return Service.START_STICKY;
  }

  // Use the service ID to keep track of our corresponding notification
  notificationId = startId;
  notificationStarted = false;

  // Get the desire title and time from the intent
  notificationTitle = intent.getStringExtra(Constants.KEY_TITLE);
  currentStepNumber = intent.getIntExtra(Constants.KEY_STEP_NUMBER, 0);
  r = intent.getParcelableExtra(Constants.KEY_RECIPE);
  int startTime = intent.getIntExtra(Constants.KEY_SECONDS, - 1);

  // Create and register a new Timer.  This will count down and broadcast the remaining time.
  timer = new Timer(this);

  // Register timer receivers
  registerReceivers();

  // Set up ringtone to alert user when timer is complete.
  Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  if (uri == null) {
    uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  }
  ringtone = RingtoneManager.getRingtone(this, uri);

  // Start the timer
  timer.start(startTime);

  // Create the notification
  updateNotification(notificationTitle, startTime);

  // Indicate that the service is running
  BrewTimerService.isRunning = true;

  return Service.START_STICKY;
}
 
源代码20 项目: GankLock   文件: LockService.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}