下面列出了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;
}
@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;
}
@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;
}
@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;
}
@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;
}
@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;
}
@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;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
return Service.START_STICKY;
}
@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;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
@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;
}
@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;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
/**
* 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;
}
@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;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}