Context startService - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Context startService method.

This code example shows how to use the following methods:startService, getDefaultSharedPreferences
import android.preference.PreferenceManager; 
 
public class NetworkReceiver extends BroadcastReceiver {
  @Override 
  public void onReceive(Context context, Intent intent) {
    SharedPreferences prefs=
        PreferenceManager.getDefaultSharedPreferences(context);
 
    if (SyncService.isSyncNeeded(context, prefs)) {
      context.startService(new Intent(context, SyncService.class));
    } 
  } 
} 
8
CodeRank
This code example shows how to use the following methods:getPackageName, getSystemService, startService
     */ 
    static void runIntentInService(Context context, Intent intent) {
        if (mWakeLock == null) {
            // This is called from BroadcastReceiver, there is no init. 
            PowerManager pm = 
                (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
                    WAKELOCK_KEY);
        } 
        mWakeLock.acquire();
        
        // Use a naming convention, similar with how permissions and intents are  
        // used. Alternatives are introspection or an ugly use of statics.  
        String receiver = context.getPackageName() + ".C2DMReceiver";
        intent.setClassName(context, receiver);
         
        context.startService(intent);
 
    } 
     

8
CodeRank
This code example shows how to use the following methods:getContentResolver, startService
 
    @Override 
    public void onReceive(final Context context, Intent intent) {
        try { 
            // Start the load average overlay, if activated 
            ContentResolver res = context.getContentResolver();
            if (Settings.Global.getInt(res, Settings.Global.SHOW_PROCESSES, 0) != 0) {
                Intent loadavg = new Intent(context, com.android.systemui.LoadAverageService.class);
                context.startService(loadavg);
            } 
        } catch (Exception e) {
            Slog.e(TAG, "Can't start load average service", e);
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:startService
    // --- system startup 
 
    @Override 
    /** Called when the system is started up */ 
    public void onReceive(Context context, Intent intent) {
        ContextManager.setContext(context);
        startReminderSchedulingService(context);
    } 
 
    public static void startReminderSchedulingService(Context context) {
        context.startService(new Intent(context, ReminderSchedulingService.class));
    } 
} 
7
CodeRank
This code example shows how to use the following methods:startService, getDefaultSharedPreferences
import android.preference.PreferenceManager; 
 
public class NetworkReceiver extends BroadcastReceiver {
  @Override 
  public void onReceive(Context context, Intent intent) {
    SharedPreferences prefs=
        PreferenceManager.getDefaultSharedPreferences(context);
 
    if (SyncService.isSyncNeeded(context, prefs)) {
      context.startService(new Intent(context, SyncService.class));
    } 
  } 
} 
8
CodeRank
This code example shows how to use the following methods:startService
 * @author alankila 
 */ 
public class BootCompletedReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, HeadsetService.class));
        if (WM8994.isSupported(context)) {
            WM8994.restore(context);
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:getPackageName, getSystemService, startService
     */ 
    static void runIntentInService(Context context, Intent intent) {
        if (mWakeLock == null) {
            // This is called from BroadcastReceiver, there is no init. 
            PowerManager pm = 
                (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
                    WAKELOCK_KEY);
        } 
        mWakeLock.acquire();
        
        // Use a naming convention, similar with how permissions and intents are  
        // used. Alternatives are introspection or an ugly use of statics.  
        String receiver = context.getPackageName() + ".C2DMReceiver";
        intent.setClassName(context, receiver);
         
        context.startService(intent);
 
    } 
     

8
CodeRank
This code example shows how to use the following methods:startService
        // Handle calendar-specific updates ourselves because they might be 
        // coming in without extras, which AppWidgetProvider then blocks. 
        final String action = intent.getAction();
        if (LOGD)
            Log.d(TAG, "AppWidgetProvider got the intent: " + intent.toString());
        if (Utils.getWidgetUpdateAction(context).equals(action)) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            performUpdate(context, appWidgetManager,
                    appWidgetManager.getAppWidgetIds(getComponentName(context)),
                    null /* no eventIds */); 
        } else if (action.equals(Intent.ACTION_PROVIDER_CHANGED)
                || action.equals(Intent.ACTION_TIME_CHANGED)
                || action.equals(Intent.ACTION_TIMEZONE_CHANGED)
                || action.equals(Intent.ACTION_DATE_CHANGED)
                || action.equals(Utils.getWidgetScheduledUpdateAction(context))) {
            Intent service = new Intent(context, CalendarAppWidgetService.class);
            context.startService(service);
        } else { 
            super.onReceive(context, intent);
        } 

7
CodeRank
This code example shows how to use the following methods:getBroadcast, startService
    public static void register(Context context,
            String senderId) {
        Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
        registrationIntent.setPackage(GSF_PACKAGE);
        registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
                PendingIntent.getBroadcast(context, 0, new Intent(), 0));
        registrationIntent.putExtra(EXTRA_SENDER, senderId);
        context.startService(registrationIntent);
        // TODO: if intent not found, notification on need to have GSF 
    } 
 
    /** 
     * Unregister the application. New messages will be blocked by server. 
     */ 
    public static void unregister(Context context) {
        Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
        regIntent.setPackage(GSF_PACKAGE);
        regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context,
                0, new Intent(), 0));
        context.startService(regIntent);
7
CodeRank
This code example shows how to use the following methods:startService
public class OnAlarmReceiver extends BroadcastReceiver {
  @Override 
  public void onReceive(Context context, Intent intent) {
    WakefulIntentService.acquireStaticLock(context);
     
    context.startService(new Intent(context, AppService.class));
  } 
} 
7
CodeRank

Related Context startService Questions & Answers:

Other Context startService Examples

27:	
28:	    // Update the widgets via the service
29:	    context.startService(intent);
30:	  }
31:	}

Read More

startService Method Overview
Request that a given application service be started.
See Code Examples for other Android Context Methods: