Example usage for android.app Activity bindService

List of usage examples for android.app Activity bindService

Introduction

In this page you can find the example usage for android.app Activity bindService.

Prototype

@Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) 

Source Link

Usage

From source file:com.zion.htf.ui.fragment.ArtistSoundcloudFragment.java

/**
 * Requests the activity to bind to the service, to allow easier communication with the service.
 * @param activity the current fragment's activity, acting as a proxy to the service
 *///from  www. jav  a2  s  . c om
private void requestServiceBond(Activity activity) {
    ArtistSoundcloudFragment.MediaPlayerServiceProxy serviceProxy = (ArtistSoundcloudFragment.MediaPlayerServiceProxy) activity;
    ((AbstractServiceProxyActivity) activity).registerObserver(this);
    activity.bindService(new Intent(activity, MediaPlayerService.class), serviceProxy.getServiceConnection(),
            Context.BIND_AUTO_CREATE);
}

From source file:com.tenforwardconsulting.cordova.BackgroundGeolocationPlugin.java

void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because there is no reason to be able to let other
    // applications replace our component.
    if (!isBound) {
        log.info("Binding to bg service");
        Activity activity = getActivity();
        Intent locationServiceIntent = new Intent(activity, LocationService.class);
        locationServiceIntent.putExtra("config", config);
        activity.bindService(locationServiceIntent, mConnection, Context.BIND_IMPORTANT);
    }/*ww w  .ja  v a2s. c om*/
}

From source file:piuk.blockchain.android.ui.SendCoinsFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Activity activity = getActivity();

    application = (WalletApplication) activity.getApplication();

    activity.bindService(new Intent(activity, BlockchainServiceImpl.class), serviceConnection,
            Context.BIND_AUTO_CREATE);
}

From source file:net.texh.cordovapluginstepcounter.CordovaStepCounter.java

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
    LOG.i(TAG, "execute()");
    Boolean result = true;//from  w  w  w  .  java  2 s .c o  m

    Activity activity = this.cordova.getActivity();
    stepCounterIntent = new Intent(activity, StepCounterService.class);

    //Get stored value for pedometerActive
    SharedPreferences sharedPref = activity.getSharedPreferences(USER_DATA_PREF, Context.MODE_PRIVATE);
    Boolean pActive = this.getPedometerIsActive(sharedPref);

    if (ACTION_CAN_COUNT_STEPS.equals(action)) {
        Boolean can = deviceHasStepCounter(activity.getPackageManager());
        Log.i(TAG, "Checking if device has step counter APIS: " + can);
        callbackContext.success(can ? 1 : 0);
    } else if (ACTION_START.equals(action)) {

        if (!pActive) {
            Log.i(TAG, "Starting StepCounterService");
            //Update pedometerActive preference
            this.setPedometerIsActive(sharedPref, true);
            activity.startService(stepCounterIntent);
        } else {
            Log.i(TAG, "StepCounterService Already Started before, just binding to it");
        }

        if (!bound) {
            Log.i(TAG, "Binding StepCounterService");
            activity.bindService(stepCounterIntent, mConnection, Context.BIND_AUTO_CREATE);
        } else {
            Log.i(TAG, "StepCounterService already binded");
        }

        //Try getting given param (starting value)
        Integer startingValue = 0;
        try {
            if (data.length() > 0) {
                startingValue = data.getInt(0);
            }
        } catch (JSONException error) {
            Log.i(TAG, "JSON EXCEPTION While Reading startingValue from 'execute' parameters (JSONArray)");
        }

        //Reset TOTAL COUNT on start
        CordovaStepCounter.setTotalCount(sharedPref, startingValue);
        callbackContext.success(startingValue);
    } else if (ACTION_STOP.equals(action)) {
        if (pActive) {
            Log.i(TAG, "Stopping StepCounterService");
            this.setPedometerIsActive(sharedPref, false);
            activity.stopService(stepCounterIntent);
        } else {
            Log.i(TAG, "StepCounterService already stopped");
        }

        if (bound) {
            Log.i(TAG, "Unbinding StepCounterService");
            activity.unbindService(mConnection);
        } else {
            Log.i(TAG, "StepCounterService already unbinded");
        }

        activity.stopService(stepCounterIntent);
        Integer currentCount = CordovaStepCounter.getTotalCount(sharedPref);
        //Callback with final count
        callbackContext.success(currentCount);
    } else if (ACTION_GET_STEPS.equals(action)) {
        if (!pActive) {
            Log.i(TAG, "Be Careful you're getting a Step count with inactive Pedometer");
        }

        Integer steps = CordovaStepCounter.getTotalCount(sharedPref);
        Log.i(TAG, "Geting steps counted from stepCounterService: " + steps);
        callbackContext.success(steps);
    } else if (ACTION_GET_TODAY_STEPS.equals(action)) {
        if (sharedPref.contains(PEDOMETER_HISTORY_PREF)) {
            String pDataString = sharedPref.getString(PEDOMETER_HISTORY_PREF, "{}");

            Date currentDate = new Date();
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            String currentDateString = dateFormatter.format(currentDate);

            JSONObject pData = new JSONObject();
            JSONObject dayData = new JSONObject();
            Integer daySteps = -1;
            try {
                pData = new JSONObject(pDataString);
                Log.d(TAG, " got json shared prefs " + pData.toString());
            } catch (JSONException err) {
                Log.d(TAG, " Exception while parsing json string : " + pDataString);
            }

            if (pData.has(currentDateString)) {
                try {
                    dayData = pData.getJSONObject(currentDateString);
                    daySteps = dayData.getInt("steps");
                } catch (JSONException err) {
                    Log.e(TAG, "Exception while getting Object from JSON for " + currentDateString);
                }
            }

            Log.i(TAG, "Getting steps for today: " + daySteps);
            callbackContext.success(daySteps);
        } else {
            Log.i(TAG, "No steps history found in stepCounterService !");
            callbackContext.success(-1);
        }
    } else if (ACTION_GET_HISTORY.equals(action)) {
        if (sharedPref.contains(PEDOMETER_HISTORY_PREF)) {
            String pDataString = sharedPref.getString(PEDOMETER_HISTORY_PREF, "{}");
            Log.i(TAG, "Getting steps history from stepCounterService: " + pDataString);
            callbackContext.success(pDataString);
        } else {
            Log.i(TAG, "No steps history found in stepCounterService !");
            callbackContext.success("{}");
        }
    } else {
        Log.e(TAG, "Invalid action called on class " + TAG + ", " + action);
        callbackContext.error("Invalid action called on class " + TAG + ", " + action);
    }

    return result;
}

From source file:com.turbulenz.turbulenz.googlepayment.java

public googlepayment(Activity activity, int purchaseRequestCode) {
    mActivity = activity;//w ww. j  ava 2  s.c o m
    mPurchaseRequestCode = purchaseRequestCode;

    // Just listens for connection / disconnection
    mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            _log("service disconnected :(");
            mService = null;
            mReady = false;
            reportReady(false);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            _log("service connected :)");
            mService = IInAppBillingService.Stub.asInterface(service);

            String packageName = mActivity.getPackageName();
            _log("checking for billing.3 in " + packageName + "...");
            try {
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (BILLING_RESPONSE_RESULT_OK == response) {
                    mReady = true;
                } else {
                    _log("billing v3 not supported for this package");
                }
            } catch (RemoteException e) {
                _error("remoteexception:");
                e.printStackTrace();
            }

            reportReady(mReady);
        }
    };

    _log("binding service ...");
    boolean bound = activity.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),
            mServiceConnection, Context.BIND_AUTO_CREATE);
    _log("back from bindService: bound: " + Boolean.toString(bound));
}