Example usage for android.content Context startService

List of usage examples for android.content Context startService

Introduction

In this page you can find the example usage for android.content Context startService.

Prototype

@Nullable
public abstract ComponentName startService(Intent service);

Source Link

Document

Request that a given application service be started.

Usage

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

public static void sendMedia(final Context context, long msgId) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_MEDIA_READY);
    i.putExtra("org.kontalk.message.msgId", msgId);
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

public static void requestRosterEntryStatus(final Context context, String to) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_ROSTER_STATUS);
    i.putExtra(MessageCenterService.EXTRA_TO, to);
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

/**
 * Tells the message center we are releasing it, allowing shutdown for
 * inactivity.//from   www  .  jav  a  2s. c o m
 */
public static void release(final Context context) {
    // decrement the application counter
    ((Kontalk) context.getApplicationContext()).release();

    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(ACTION_RELEASE);
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

public static void requestVersionInfo(final Context context, String to, String id) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_VERSION);
    i.putExtra(EXTRA_TO, to);/* w  ww . j  a v  a2  s  .  co m*/
    i.putExtra(EXTRA_PACKET_ID, id);
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

public static void requestPublicKey(final Context context, String to, String id) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_PUBLICKEY);
    i.putExtra(EXTRA_TO, to);/*from w  ww.j  a  v a  2  s . c  o  m*/
    i.putExtra(EXTRA_PACKET_ID, id);
    context.startService(i);
}

From source file:im.neon.activity.CommonActivityUtils.java

/**
 * Start the events stream service.//w w  w  .  j  av  a2 s .  co  m
 *
 * @param context the context.
 */
public static void startEventStreamService(Context context) {
    // the events stream service is launched
    // either the application has never be launched
    // or the service has been killed on low memory
    if (EventStreamService.getInstance() == null) {
        ArrayList<String> matrixIds = new ArrayList<>();
        Collection<MXSession> sessions = Matrix.getInstance(context.getApplicationContext()).getSessions();

        if ((null != sessions) && (sessions.size() > 0)) {
            Log.d(LOG_TAG, "restart EventStreamService");

            for (MXSession session : sessions) {
                boolean isSessionReady = session.getDataHandler().getStore().isReady();

                if (!isSessionReady) {
                    session.getDataHandler().getStore().open();
                }

                // session to activate
                matrixIds.add(session.getCredentials().userId);
            }

            Intent intent = new Intent(context, EventStreamService.class);
            intent.putExtra(EventStreamService.EXTRA_MATRIX_IDS,
                    matrixIds.toArray(new String[matrixIds.size()]));
            intent.putExtra(EventStreamService.EXTRA_STREAM_ACTION,
                    EventStreamService.StreamAction.START.ordinal());
            context.startService(intent);
        }
    }
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

/** Broadcasts our presence to the server. */
public static void updateStatus(final Context context) {
    // FIXME this is what sendPresence already does
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(ACTION_PRESENCE);/*from  w w  w  .j  a va2s.c  om*/
    i.putExtra(EXTRA_STATUS, Preferences.getStatusMessage());
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

public static void requestLastActivity(final Context context, String to, String id) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_LAST_ACTIVITY);
    i.putExtra(EXTRA_TO, to);/*from   ww w.ja v  a 2 s.c o  m*/
    i.putExtra(EXTRA_PACKET_ID, id);
    context.startService(i);
}

From source file:org.kontalk.service.msgcenter.MessageCenterService.java

/** Replies to a presence subscription request. */
public static void replySubscription(final Context context, String to, int action) {
    Intent i = new Intent(context, MessageCenterService.class);
    i.setAction(MessageCenterService.ACTION_SUBSCRIBED);
    i.putExtra(EXTRA_TO, to);//  w ww  .  j a v  a2s .  co m
    i.putExtra(EXTRA_PRIVACY, action);
    context.startService(i);
}

From source file:com.nick.scalpel.core.AutoBindWirer.java

@Override
public void wire(final Context context, final Object object, final Field field) {
    ReflectionUtils.makeAccessible(field);
    Object fieldObject = ReflectionUtils.getField(field, object);
    if (fieldObject != null)
        return;/*from  w  ww  .j a va  2s. c om*/

    // FIXME: 21/03/16 Ensure it is an AIDL service.
    boolean isIInterface = field.getType().isInterface();
    Preconditions.checkState(isIInterface,
            "Field:" + field.getName() + " is not an AIDL interface, is:" + field.getType());

    AutoBind autoBind = field.getAnnotation(AutoBind.class);
    String action = autoBind.action();
    String pkg = autoBind.pkg();
    int flags = autoBind.flags();
    String callback = autoBind.callback();
    boolean startService = autoBind.startService();
    boolean isExplicit = !TextUtils.isEmpty(action) && !TextUtils.isEmpty(pkg);
    Preconditions.checkState(isExplicit, "Action and PackageName should be specified");

    boolean autoUnbind = autoBind.autoUnbind();
    boolean isActivity = object instanceof Activity;
    Preconditions.checkState(!autoUnbind || isActivity, "Auto unbind only work for activities.");

    AutoBind.Callback callbackInstance = null;
    if (!TextUtils.isEmpty(callback)) {
        Object callbackObject = null;
        switch (callback) {
        case "this":
            callbackObject = object;
            break;
        default:
            Field callbackField = ReflectionUtils.findField(object, callback);
            if (callbackField != null) {
                ReflectionUtils.makeAccessible(callbackField);
                callbackObject = ReflectionUtils.getField(callbackField, object);
            }
        }
        boolean isCallback = callbackObject instanceof AutoBind.Callback;
        Preconditions.checkState(isCallback, "Field:" + callback + " is not an instance of Callback.");
        callbackInstance = (AutoBind.Callback) callbackObject;
    }

    final Intent intent = new Intent(action);
    intent.setPackage(pkg);

    if (startService)
        context.startService(intent);

    final AutoBind.Callback finalCallbackInstance = callbackInstance;
    final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Class serviceClass = field.getType();
            String stubClassName = serviceClass.getName() + "$Stub";
            try {
                Class stubClass = Class.forName(stubClassName);
                Method asInterface = ReflectionUtils.findMethod(stubClass, "asInterface", IBinder.class);
                Object result = ReflectionUtils.invokeMethod(asInterface, null, service);
                ReflectionUtils.setField(field, object, result);
                // Callback result.
                if (finalCallbackInstance != null)
                    finalCallbackInstance.onServiceBound(name, this, intent);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (finalCallbackInstance != null)
                finalCallbackInstance.onServiceDisconnected(name);
        }
    };
    //noinspection ResourceType
    context.bindService(intent, connection, flags);

    if (autoUnbind) {
        final String fieldName = field.getName();
        boolean registered = mLifeCycleManager
                .registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
                    @Override
                    public void onActivityDestroyed(Activity activity) {
                        super.onActivityDestroyed(activity);
                        if (activity == object) {
                            logV("unBind service for: " + fieldName);
                            context.unbindService(connection);
                            mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
                        }
                    }
                });
        if (!registered) {
            logE("Failed to register life cycle callback!");
        }
    }
}