下面列出了android.content.Context#startService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Adds a new SIP account.
* @param context application context
* @param sipAccount sip account data
* @return sip account ID uri as a string
*/
public static String setAccount(Context context, SipAccountData sipAccount) {
if (sipAccount == null) {
throw new IllegalArgumentException("sipAccount MUST not be null!");
}
String accountID = sipAccount.getIdUri();
checkAccount(accountID);
Intent intent = new Intent(context, SipService.class);
intent.setAction(ACTION_SET_ACCOUNT);
intent.putExtra(PARAM_ACCOUNT_DATA, sipAccount);
context.startService(intent);
return accountID;
}
public void startService(final Context context, final Service... services) {
Thread serviceThread = new Thread(new Runnable() {
@Override
public void run() {
for (Service s : services) {
if (!ServiceUtils.isServiceRunning(context, s.getClass()
.getName())) {
Intent i = new Intent(context, s.getClass());
context.startService(i);
ZogUtils.printLog(ServiceManager.class,
"start service " + s.getClass().getName());
}
}
}
});
serviceThread.start();
}
public static void setAlert(long id, double latitude, double longitude, long radius, Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
context.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Intent proximity = new Intent(context, BackgroundService.class);
proximity.setAction(BackgroundService.ACTION_PROXIMITY);
proximity.putExtra(BackgroundService.EXTRA_WAYPOINT, id);
PendingIntent pi = PendingIntent.getService(context, 100 + (int) id, proximity, PendingIntent.FLAG_UPDATE_CURRENT);
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Log.i(TAG, "Set proximity waypoint=" + id + " radius=" + radius);
if (radius == 0) {
lm.removeProximityAlert(pi);
// Send proximity exit
Intent exit = new Intent(context, BackgroundService.class);
exit.setAction(BackgroundService.ACTION_PROXIMITY);
exit.putExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
exit.putExtra(BackgroundService.EXTRA_WAYPOINT, id);
context.startService(exit);
} else
lm.addProximityAlert(latitude, longitude, radius, -1, pi);
new DatabaseHelper(context).setProximity(id, radius).close();
}
}
private void startServiceCommand(Context context, String cmd, String mess) {
Intent i = new Intent(context, StatusUpdateService.class);
i.putExtra("commandtype", cmd);
i.putExtra("message", mess);
context.startService(i);
}
@Override
public void onNotificationMessageClicked(Context context, MixPushMessage message) {
message.setNotify(1);
Intent intent = new Intent(MixPushMessageReceiver.NOTIFICATION_CLICKED);
intent.putExtra(MixPushMessageReceiver.MESSAGE, message);
context.sendBroadcast(intent, sReceiverPermission);
Log.d("onNotificationClicked", message.getContent());
if (sMixPushIntentServiceClass != null){
intent.setClass(context,sMixPushIntentServiceClass);
context.startService(intent);
}
}
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent intent = new Intent(context.getApplicationContext(), UpcomingMovieUpdateService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.startService(intent);
}
/**
* When we receive the broadcast callback we extract the required service and start it.
* The framework only releases the wakelock when onReceive returns.
*/
@SuppressWarnings("ConstantConditions")
@Override
public void onReceive(final Context context, final Intent broadcastIntent) {
JoH.getWakeLock(TAG, 1000); // deliberately not released
final String serviceName = broadcastIntent.getStringExtra(SERVICE_PARAMETER);
UserError.Log.d(TAG, "Trampoline ignition for: " + serviceName);
if (serviceName == null) {
UserError.Log.wtf(TAG, "Incorrectly passed pending intent with null service parameter!");
return;
}
final Class serviceClass = getClassFromName(serviceName);
if (serviceClass == null) {
UserError.Log.wtf(TAG, "Could not resolve service class for: " + serviceName);
return;
}
final Intent serviceIntent = new Intent(context, serviceClass);
final String function = broadcastIntent.getStringExtra("function");
if (function != null) serviceIntent.putExtra("function", function);
ComponentName startResult;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& BuildConfig.targetSDK >= Build.VERSION_CODES.N
&& ForegroundServiceStarter.shouldRunCollectorInForeground()) {
try {
UserError.Log.d(TAG, String.format("Starting oreo foreground service: %s", serviceIntent.getComponent().getClassName()));
} catch (NullPointerException e) {
UserError.Log.d(TAG, "Null pointer exception in startServiceCompat");
}
startResult = context.startForegroundService(serviceIntent);
} else {
startResult = context.startService(serviceIntent);
}
if (D) UserError.Log.d(TAG, "Start result: " + startResult);
}
@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);
}
}
/**
* Starts this service to perform action Single Sync with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionSingleSync(Context context, int numOfPages) {
Intent intent = new Intent(context, SyncingService.class);
intent.setAction(ACTION_SYNC);
intent.putExtra(SYNC_PERIOD, numOfPages);
context.startService(intent);
}
/**
* @param context 停止ForReplace
*/
public static void stopForReplace(Context context) {
if (running) {
Intent intent = new Intent(context, ReadAloudService.class);
intent.setAction(ActionDoneService);
intent.putExtra("replace","1");
context.startService(intent);
}
}
/**
* 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
*/
// TODO: Customize helper method
public static void startActionExtractApk(Context context, String param1, HashMap<String, List<String>> packageNameToSplitApksMapping) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_EXPORT_APK);
intent.putExtra(PACKAGENAME, param1);
intent.putExtra(PACKAGE_NAME_TO_SPLIT_APKS_MAPPING, packageNameToSplitApksMapping);
context.startService(intent);
}
/**
* Mutes and Un-Mutes video for a call. If the call does not exist or has been terminated, a disconnected
* state will be sent to
* {@link BroadcastEventReceiver#onCallState(String, int, pjsip_inv_state, pjsip_status_code, long, boolean, boolean, boolean)}
* @param context application context
* @param accountID account ID
* @param callID call ID
* @param mute whether to mute or un-mute the video
*/
public static void setVideoMute(Context context, String accountID, int callID, boolean mute) {
checkAccount(accountID);
Intent intent = new Intent(context, SipService.class);
intent.setAction(ACTION_SET_VIDEO_MUTE);
intent.putExtra(PARAM_ACCOUNT_ID, accountID);
intent.putExtra(PARAM_CALL_ID, callID);
intent.putExtra(PARAM_VIDEO_MUTE, mute);
context.startService(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo network = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (network != null && network.isConnected()) {
if (Settings.needToRegister(context)) {
context.startService(new Intent(context, RegistrationIntentService.class));
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received a notification intent in the NotificationService's receiver.");
// TODO(peter): Do we need to acquire a wake lock here?
intent.setClass(context, NotificationService.class);
context.startService(intent);
}
public static void start(Context context, QBChatDialog chatDialog) {
Intent intent = new Intent(QBServiceConsts.LEAVE_GROUP_DIALOG_ACTION, null, context, QBService.class);
intent.putExtra(QBServiceConsts.EXTRA_DIALOG, chatDialog);
context.startService(intent);
}
public static void cancelTask(Context context)
{
Intent i = new Intent(context, FileOpsService.class);
i.setAction(ACTION_CANCEL_TASK);
context.startService(i);
}
public static void start(final Context context) {
context.startService(new Intent(context, QueueService.class));
}
public static void startAppSearchService(Context context){
Intent intent=new Intent(context,AppSearchService.class);
intent.setAction(AppSearchService.ACTION_APP_SEARCH_SERVICE);
context.startService(intent);
}
/**
* Starts the service and store the listener to be notified when a new incoming or outgoing sms be processed
* inside the SMS content provider
*
* @param context used to start the service
* @param smsListener to notify when the sms content provider gets a new sms
*/
public static void initializeSmsRadarService(Context context, SmsListener smsListener) {
SmsRadar.smsListener = smsListener;
Intent intent = new Intent(context, SmsRadarService.class);
context.startService(intent);
}
/**
* Starts this service to check if there are any HPROF files to process and if any are found,
* start converting them to BMD.
*
* @see IntentService
*/
public static void processHprofDumps(@NonNull Context context) {
Intent intent = new Intent(context, CruncherService.class);
intent.setAction(ACTION_CHECK_FILES);
context.startService(intent);
}