[LocalServiceBinding.java]

    private boolean mIsBound;
    private LocalService mBoundService;

.
.
.
  private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {

            mBoundService = ((LocalService.LocalBinder)service).getService();
           
            Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected,
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {

            mBoundService = null;
            Toast.makeText(LocalServiceBinding.this, R.string.local_service_disconnected,
                    Toast.LENGTH_SHORT).show();
        }
    };

    private OnClickListener mBindListener = new OnClickListener() {
        public void onClick(View v) {

            bindService(new Intent(LocalServiceBinding.this,
                    LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }
    };

    private OnClickListener mUnbindListener = new OnClickListener() {
        public void onClick(View v) {
            if (mIsBound) {
                unbindService(mConnection);
                mIsBound = false;
            }
        }
    };



[LocalService.java]

private NotificationManager mNM;

      public class LocalBinder extends Binder {
        LocalService getService() {
            return LocalService.this;
        }

    }
   
    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        showNotification();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
 mNM.cancel(R.string.local_service_started);

 Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    private void showNotification() {
        CharSequence text = getText(R.string.local_service_started);
        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, LocalServiceController.class), 0);

        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        mNM.notify(R.string.local_service_started, notification);
    }
}

'old > API_Demo' 카테고리의 다른 글

APP_Service_RemoteServiceController  (0) 2010.03.25
APP_Service_LocalServiceController  (0) 2010.03.25
APP_Service_ForegroundServiceController  (0) 2010.03.24
APP_Search  (0) 2010.03.24
APP_Preferences_PreferencesFromCode  (0) 2010.03.24
Posted by jazzlife
,