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.wso2.iot.agent.services.AgentStartupReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, "Action received: " + action);
    }/*  w  ww.  j  ava2 s  .  co  m*/
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action) && !isNetworkConnected(context, intent)) {
        return;
    }
    if (!Preference.getBoolean(context, Constants.PreferenceFlag.DEVICE_ACTIVE)) {
        Log.e(TAG, "Device is not active");
        return;
    }
    setRecurringAlarm(context.getApplicationContext());
    if (!EventRegistry.eventListeningStarted) {
        EventRegistry registerEvent = new EventRegistry(context.getApplicationContext());
        registerEvent.register();
    }
    if (!CommonUtils.isServiceRunning(context, LocationService.class)) {
        Intent serviceIntent = new Intent(context, LocationService.class);
        context.startService(serviceIntent);
    }
    if (Intent.ACTION_BOOT_COMPLETED.equals(action)
            || Constants.AGENT_UPDATED_BROADCAST_ACTION.equals(action)) {
        if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
            Preference.putBoolean(context.getApplicationContext(), Constants.AGENT_FRESH_START, true);
            Intent i = new Intent(context, SplashActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

From source file:rtandroid.benchmark.ui.BenchmarkFragment.java

private void startBenchmark() {
    Set<TestCase> selectedCases = mTestCaseAdapter.getSelectedTestCases();

    // Abort if no test cases were selected
    if (selectedCases.isEmpty()) {
        new AlertDialog.Builder(getActivity()).setTitle(R.string.dialog_missing_test_case_title)
                .setMessage(R.string.dialog_missing_test_case_msg).setPositiveButton(android.R.string.ok, null)
                .show();/*from w  w w.j av a 2s.c o m*/
        return;
    }

    // Add a warmup case
    selectedCases.add(
            new TestCase("Warmup Phase", TestCase.NO_PRIORITY, TestCase.NO_POWER_LEVEL, TestCase.NO_CORE_LOCK));

    // Notify listener
    if (mListener != null) {
        mListener.onBenchmarkStart(mConfig);
    }

    // Show dialog
    DialogFragment dialog = ProgressDialog.newInstance(mConfig.getBenchmark().getName(), selectedCases.size(),
            mConfig.Cycles);
    dialog.setTargetFragment(this, 0);
    dialog.show(getFragmentManager(), null);

    // Start service and queue all benchmarks
    Context context = getActivity();
    Intent intent = new Intent(context, BenchmarkService.class);
    intent.putExtra(BenchmarkService.EXTRA_BENCHMARK, mConfig.BenchmarkIdx);
    intent.putExtra(BenchmarkService.EXTRA_PARAMETER, mConfig.Parameter);
    intent.putExtra(BenchmarkService.EXTRA_CYCLES, mConfig.Cycles);
    intent.putExtra(BenchmarkService.EXTRA_SLEEP, mConfig.SleepMs);

    Gson gson = new Gson();
    for (TestCase testCase : selectedCases) {
        String jsonTestCase = gson.toJson(testCase, TestCase.class);
        intent.putExtra(BenchmarkService.EXTRA_TEST_CASE, jsonTestCase);
        context.startService(intent);
    }
}

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

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<String>();
        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();
                }//from   w ww  .  j  ava  2s .  com

                // 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:com.juick.android.MainActivity.java

public static void toggleXMPP(Context ctx) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx);
    boolean useXMPP = sp.getBoolean("useXMPP", false);
    if (useXMPP) {
        ctx.startService(new Intent(ctx, XMPPService.class));
    } else {//from w ww. j a  v a 2  s  . com
        if (isXMPPServiceRunning(ctx)) {
            Intent service = new Intent(ctx, XMPPService.class);
            service.putExtra("terminate", true);
            ctx.startService(service);
        }
    }
}

From source file:com.mikecorrigan.bohrium.pubsub.RegistrationClient.java

private void c2dmUnregister(Context context) {
    Log.d(TAG, "c2dmUnregister: context=" + context);

    Intent intent = new Intent(REQUEST_UNREGISTRATION_INTENT);
    intent.setPackage(GSF_PACKAGE);//from w  w  w .  ja v  a 2 s.  co m
    intent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context, 0, new Intent(), 0));
    ComponentName name = context.startService(intent);
    if (name == null) {
        // Service not found.
        setStateAndNotify(REGISTRATION_STATE_ERROR, REGISTRATION_SUBSTATE_ERROR_C2DM_NOT_FOUND);
    }
}

From source file:eu.inmite.apps.smsjizdenka.util.NotificationUtil.java

/**
 * Posts notification about new sms ticket.
 *
 * @param c context to post notification
 * @param t new ticket//from w w  w .ja v a 2s .  co  m
 */
public static void notifyTicket(Context c, Ticket t, boolean keepNotification) {
    String text;
    String ticker;
    int smallIcon;
    int largeIcon;
    int status;
    switch (TicketsAdapter.getValidityStatus(t.getStatus(), t.getValidTo())) {
    case TicketProvider.Tickets.STATUS_VALID:
    case TicketProvider.Tickets.STATUS_VALID_EXPIRING:
        text = c.getString(R.string.notif_valid_text, FormatUtil.formatDateTimeDifference(t.getValidTo()));
        ticker = c.getString(R.string.notif_valid_ticker);
        smallIcon = R.drawable.notification_small_ready;
        largeIcon = R.drawable.notification_big_ready;
        status = TicketProvider.Tickets.STATUS_VALID_EXPIRING;
        break;
    case TicketProvider.Tickets.STATUS_EXPIRING:
    case TicketProvider.Tickets.STATUS_EXPIRING_EXPIRED:
        text = c.getString(R.string.notif_expiring_text, FormatUtil.formatTime(t.getValidTo()));
        ticker = c.getString(R.string.notif_expiring_ticker);
        smallIcon = R.drawable.notification_small_warning;
        largeIcon = R.drawable.notification_big_warning;
        status = TicketProvider.Tickets.STATUS_EXPIRING_EXPIRED;
        break;
    case TicketProvider.Tickets.STATUS_EXPIRED:
        text = c.getString(R.string.notif_expired_text, FormatUtil.formatTime(t.getValidTo()));
        ticker = c.getString(R.string.notif_expired_ticker);
        smallIcon = R.drawable.notification_small_expired;
        largeIcon = R.drawable.notification_big_expired;
        status = TicketProvider.Tickets.STATUS_EXPIRED;
        break;
    default:
        return;
    }

    Intent intent = new Intent(c, WearableService.class);
    intent.setAction("sent_notification_to_wear");
    intent.putExtra("ticket", t);
    intent.putExtra("status", status);
    c.startService(intent);

    Intent i = new Intent(c, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra(MainActivity.EXTRA_TICKET_ID, t.getId());
    PendingIntent openIntent = PendingIntent.getActivity(c, t.getNotificationId(), i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Intent i2 = new Intent(c, MainActivity.class);
    i2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i2.putExtra(MainActivity.EXTRA_TICKET_ID, t.getId());
    i2.putExtra(MainActivity.EXTRA_SHOW_SMS, true);
    PendingIntent showSmsIntent = PendingIntent.getActivity(c, t.getNotificationId() + 1000, i2,
            PendingIntent.FLAG_CANCEL_CURRENT);
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action(R.drawable.notification_show_sms, R.string.notif_show_sms, showSmsIntent));
    List<String> rows = new ArrayList<String>();
    rows.add(text);
    rows.add(c.getString(R.string.tickets_valid_from) + ": " + FormatUtil.formatDateTime(t.getValidFrom()));
    rows.add(c.getString(R.string.tickets_code) + ": " + t.getHash());
    fireNotification(c, t.getNotificationId(), openIntent, c.getString(R.string.application_name), text, rows,
            t.getCity(), ticker, smallIcon, largeIcon, actions, keepNotification);
}

From source file:com.gcm.client.GcmHelper.java

/**
 * Subscribe to a list of topics./*from   w  ww . j  a  v a  2s  .  co m*/
 *
 * @param context An instance of the application {@link Context}
 * @return true if it is a new topic
 */
public boolean subscribeTopic(@NonNull Context context, @NonNull String[] newTopics) {
    if (!initialized)
        init(context);
    if (newTopics.length == 0)
        return false;
    if (null == topics) {
        topics = new ArrayList<>();
    }
    for (String topic : newTopics) {
        if (topics.contains(topic)) {
            return false;
        }
        topics.add(topic);
    }
    saveSubscibedTopics(context);
    Intent intent = new Intent(context, RegistrationIntentService.class);
    intent.setAction(RegistrationIntentService.ACTION_SUBSCRIBE);
    intent.putExtra(RegistrationIntentService.EXTRA_TOPIC_LIST, newTopics);
    context.startService(intent);
    return true;
}

From source file:com.aware.Aware.java

/**
 * Requests the download of a plugin given the package name from AWARE webservices.
 * @param context//from ww w. ja  va  2 s  .c  om
 * @param package_name
 * @param is_update
 */
public static void downloadPlugin(Context context, String package_name, boolean is_update) {
    Intent pluginIntent = new Intent(context, DownloadPluginService.class);
    pluginIntent.putExtra("package_name", package_name);
    pluginIntent.putExtra("is_update", is_update);
    context.startService(pluginIntent);
}

From source file:com.android.tripgenie.auto.MessageReplyReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    if (MessagingService.REPLY_ACTION.equals(intent.getAction())) {
        final int conversationId = intent.getIntExtra(MessagingService.CONVERSATION_ID, -1);
        CharSequence reply = getMessageText(intent);
        String response = "";
        if (conversationId != -1) {
            Log.d(TAG, "Got reply (" + reply + ") for ConversationId " + conversationId);

            //pass the reply to MasterBot
            MasterBot masterBot = MyApplication.getInstance().getMasterBot();

            BotRunnable botRunnable = new BotRunnable() {
                @Override/*w ww.j  av a 2s  .  co m*/
                public void run() {
                    // Tell the Service to send another message.
                    Intent serviceIntent = new Intent(context, MessagingService.class);
                    serviceIntent.setAction(MessagingService.SEND_MESSAGE_ACTION);
                    if (!TextUtils.isEmpty(getData())) {
                        serviceIntent.putExtra("message", getData());
                    }
                    context.startService(serviceIntent);
                    Log.d(TAG, "Sending response for ConversationId " + conversationId);
                }
            };

            masterBot.process(String.valueOf(reply), botRunnable);

            //ApiHandler apiHandler = new ApiHandler();
            //apiHandler.handleUrl("reply");
        }
    }
}