Example usage for android.content ServiceConnection ServiceConnection

List of usage examples for android.content ServiceConnection ServiceConnection

Introduction

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

Prototype

ServiceConnection

Source Link

Usage

From source file:com.jetheis.android.makeitrain.billing.googleplay.GooglePlayBillingWrapper.java

private GooglePlayBillingWrapper(Context context, OnGooglePlayBillingReadyListener onReadyListener,
        OnGooglePlayVipModePurchaseFoundListener onPurchaseListener) {
    mContext = context;//from   www. j  a  v  a  2 s  . com
    mOnReadyListener = onReadyListener;
    mOnPurchaseListnener = onPurchaseListener;

    context.startService(new Intent(context, GooglePlayBillingService.class));

    mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBoundService = ((GooglePlayBillingService.GooglePlayBillingBinder) service).getService();
            mBoundService.checkIsBillingSupported(new OnGooglePlayBillingSupportResultListener() {

                @Override
                public void onGooglePlayBillingSupportResultFound(boolean billingSupported) {
                    if (billingSupported) {
                        Log.i(Constants.TAG, "Google Play billing ready");
                        if (mOnReadyListener != null) {
                            mOnReadyListener.onGooglePlayBillingReady();
                        }
                    } else {
                        Log.i(Constants.TAG, "Google Play billing is not supported");
                        if (mOnReadyListener != null) {
                            mOnReadyListener.onGooglePlayBillingNotSupported();
                        }
                    }
                }

            });

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBoundService = null;
        }

    };

    mContext.bindService(new Intent(context, GooglePlayBillingService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}

From source file:com.arcusapp.soundbox.fragment.PlayFragment.java

private void bindMediaPlayerService() {
    myServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
            mediaService = ((MediaPlayerService.MyBinder) binder).getService();
            mediaService.registerListener(serviceListener);
            updateUI();//from  ww w. j  a v a  2 s .  c o  m
            initRunnableSeekBar();
        }

        public void onServiceDisconnected(ComponentName className) {
            mediaService = null;
        }
    };

    Intent serviceIntent = new Intent(SoundBoxApplication.ACTION_MEDIA_PLAYER_SERVICE, null,
            SoundBoxApplication.getContext(), MediaPlayerService.class);
    getActivity().bindService(serviceIntent, myServiceConnection, Context.BIND_AUTO_CREATE);
}

From source file:jp.app_mart.billing.AppmartHelper.java

/**
 * appmart???/*from w  w w.  j a  va  2 s  .co  m*/
 * @param listener Activity?callback
 */
public void startSetup(final OnAppmartSetupFinishedListener listener) {

    if (mSetupDone)
        throw new IllegalStateException(mContext.getString(R.string.already_connected));

    mServiceConn = new ServiceConnection() {

        //?
        public void onServiceConnected(ComponentName name, IBinder boundService) {
            mService = AppmartInBillingInterface.Stub.asInterface((IBinder) boundService);
            mSetupDone = true;
            if (listener != null) {
                listener.onAppmartSetupFinished(new AppmartResult(BILLING_RESPONSE_RESULT_OK,
                        mContext.getString(R.string.is_now_connected)));
            }

            //????receiver
            IntentFilter filter = new IntentFilter("appmart_broadcast_return_service_payment");
            if (mReceiver == null)
                mReceiver = new AppmartReceiver();
            mContext.registerReceiver(mReceiver, filter);
        }

        //?
        public void onServiceDisconnected(ComponentName name) {
            logDebug(mContext.getString(R.string.is_now_deconnected));
            mService = null;
        }
    };

    Intent serviceIntent = new Intent();
    serviceIntent.setClassName(BILLING_APPMART_PACKAGE_NAME, BILLING_APPMART_SERVICE_NAME);

    //?appmart????????
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {

        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);

    } else {

        if (listener != null) {
            listener.onAppmartSetupFinished(new AppmartResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    mContext.getString(R.string.appmart_not_installed)));
        }
    }
}

From source file:leoisasmendi.android.com.suricatepodcast.MainActivity.java

private void initServiceConnection() {
    //Binding this Client to the AudioPlayer Service
    serviceConnection = new ServiceConnection() {
        @Override/*from  w  w w  .  ja v a  2 s .  c om*/
        public void onServiceConnected(ComponentName name, IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            MediaPlayerService.LocalBinder binder = (MediaPlayerService.LocalBinder) service;
            player = binder.getService();
            serviceBound = true;

            Log.d(TAG, "onServiceConnected: Service bound");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            serviceBound = false;
        }
    };
}

From source file:paulscode.android.mupen64plusae.ExtractTexturesFragment.java

private void actuallyExtractTextures(Activity activity) {
    mInProgress = true;/*from   w ww  . j  a  v a2  s  .co m*/

    CharSequence title = getString(R.string.pathHiResTexturesTask_title);
    CharSequence message = getString(R.string.toast_pleaseWait);
    mProgress = new ProgressDialog(mProgress, getActivity(), title, mTexturesZipPath.getAbsolutePath(), message,
            true);
    mProgress.show();

    /** Defines callbacks for service binding, passed to bindService() */
    mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {

            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            ExtractTexturesService extractTexturesService = binder.getService();
            extractTexturesService.setExtractTexturesListener(ExtractTexturesFragment.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            //Nothing to do here
        }
    };

    // Asynchronously extract textures
    ActivityHelper.startExtractTexturesService(activity.getApplicationContext(), mServiceConnection,
            mTexturesZipPath.getAbsolutePath());
}

From source file:edu.asu.cse535.assignment3.MainActivity.java

public void onStartRecordingRunning(View v) {
    Log.w(this.getClass().getSimpleName(), "Button is clicked");
    intent = new Intent(MainActivity.this.getBaseContext(), AccIntentService.class);
    intent.putExtra("activity", Constants.ACTIVITY_RUNNING);
    startService(intent);/*from  ww  w . j a va  2  s .com*/
    serve = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            accIntentService = ((AccIntentService.LocalBinder) service).getInstance();
            accIntentService.setHandler(handler);
            Log.w(this.getClass().getSimpleName(), "Activity is connected to service");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    bindService(intent, serve, Context.BIND_AUTO_CREATE);
}

From source file:com.perm.DoomPlay.AbstractReceiver.java

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

    prepareLang();/*  w w  w  . j  av  a  2  s .  c  om*/
    prepareActionBar();
    createBroadcastRec();
    initializeKiller();

    intentService = new Intent(this, PlayingService.class);

    serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            playingService = ((PlayingService.MyBinder) binder).getService();
            onServiceBound();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
}

From source file:org.chirpradio.mobile.Playing.java

void doBindService() {
    Intent serviceIntent = new Intent(this, PlaybackService.class);
    serviceConnection = new ServiceConnection() {
        @Override/*from   ww w. j  a  v a2  s.  com*/
        public void onServiceConnected(ComponentName name, IBinder service) {
            playbackService = ((PlaybackService.PlaybackBinder) service).getService();
            Debug.log(this, "CONNECTED");
            setupPlaybackListeners();
            if (playbackService != null) {
                if (playbackService.isPlaying()) {
                    Debug.log(this, "changing button to stop icon");
                    //playButton.setImageResource(R.drawable.stop);
                    playButton.setText("Stop");
                    //playButton.setEnabled(false);
                    //stopButton.setEnabled(true);
                } else {
                    Debug.log(this, "changing button to play icon");
                    //playButton.setImageResource(R.drawable.play);
                    playButton.setText("Play");
                    //playButton.setEnabled(true);
                    //stopButton.setEnabled(false);
                }
            } else {
                Debug.log(this, "playbackService is null in onServiceConnected");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Debug.log(this, "DISCONNECT");
            // playbackService = null;
        }
    };
    getApplicationContext().startService(serviceIntent);
    getApplicationContext().bindService(serviceIntent, serviceConnection, 0);

    serviceIsBound = true;
}

From source file:com.davidmiguel.gobees.monitoring.MonitoringFragment.java

@Nullable
@Override/*from  www  . j  ava  2  s .  c  om*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.monitoring_frag, container, false);

    // Configure OpenCV callback
    loaderCallback = new BaseLoaderCallback(getContext()) {
        @Override
        public void onManagerConnected(final int status) {
            if (status == LoaderCallbackInterface.SUCCESS) {
                presenter.onOpenCvConnected();
            } else {
                super.onManagerConnected(status);
            }
        }
    };

    // Don't switch off screen
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // Configure camera
    cameraView = (CameraView) root.findViewById(R.id.camera_view);
    cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
    cameraView.setMaxFrameSize(MAX_WIDTH, MAX_HEIGHT);
    // Configure view
    numBeesTV = (TextView) root.findViewById(R.id.num_bees);
    settingsLayout = (RelativeLayout) getActivity().findViewById(R.id.settings);
    chronometer = (Chronometer) root.findViewById(R.id.chronometer);

    // Configure icons
    settingsIcon = (ImageView) root.findViewById(R.id.settings_icon);
    settingsIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            presenter.openSettings();
        }
    });
    recordIcon = (ImageView) root.findViewById(R.id.record_icon);
    recordIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            presenter.startMonitoring();
        }
    });

    // Configure service connection
    mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            MonitoringBinder binder = (MonitoringBinder) service;
            mService = binder.getService(new GoBeesDataSource.SaveRecordingCallback() {
                @Override
                public void onRecordingTooShort() {
                    // Finish activity with error
                    Intent intent = new Intent();
                    intent.putExtra(HiveRecordingsFragment.ARGUMENT_MONITORING_ERROR,
                            HiveRecordingsFragment.ERROR_RECORDING_TOO_SHORT);
                    getActivity().setResult(Activity.RESULT_CANCELED, intent);
                    getActivity().finish();

                }

                @Override
                public void onSuccess() {
                    // Finish activity with OK
                    getActivity().setResult(Activity.RESULT_OK);
                    getActivity().finish();
                }

                @Override
                public void onFailure() {
                    // Finish activity with error
                    Intent intent = new Intent();
                    intent.putExtra(HiveRecordingsFragment.ARGUMENT_MONITORING_ERROR,
                            HiveRecordingsFragment.ERROR_SAVING_RECORDING);
                    getActivity().setResult(Activity.RESULT_CANCELED, intent);
                    getActivity().finish();
                }
            });
            // Set chronometer
            chronometer.setBase(mService.getStartTime());
            chronometer.setVisibility(View.VISIBLE);
            chronometer.start();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;
        }
    };
    return root;
}