상세 컨텐츠

본문 제목

[Android]내부 바인드 서비스

Android 개발

by mobile 2013. 9. 21. 02:08

본문

반응형

- 바인드 서비스 생명 주기

// MainActivity.java

package com.example.hellobindservice;


import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;


public class MainActivity extends Activity {

private boolean mIsBound;

private final ServiceConnection mConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, R.string.local_service_disconnected, Toast.LENGTH_SHORT).show();

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// TODO Auto-generated method stub

LocalService local = ((LocalService.LocalBinder)service).getService();

Toast.makeText(MainActivity.this, R.string.local_service_connected, Toast.LENGTH_SHORT).show();

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.bind);

button.setOnClickListener(mBindListener);

button = (Button)findViewById(R.id.unbind);

button.setOnClickListener(mUnbindListener);

}

private final OnClickListener mBindListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

doBindService();

}

};

private final OnClickListener mUnbindListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

doUnbnidService();

}

};

void doBindService() {

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

}

void doUnbnidService() {

if(mIsBound) {

unbindService(mConnection);

mIsBound = false;

}

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}



// LocalService.java

package com.example.hellobindservice;


import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;


public class LocalService extends Service {


private NotificationManager mNM;

private final IBinder mBinder = new LocalBinder();


public class LocalBinder extends Binder {

LocalService getService() {

return LocalService.this;

}

}


@Override

public void onCreate() {

// TODO Auto-generated method stub

// super.onCreate();

mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

showNotification();

Toast.makeText(this, "service call onCreate() 메스드", Toast.LENGTH_SHORT)

.show();

}


@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

// return super.onStartCommand(intent, flags, startId);

Log.i("LocalService", "Received start id " + startId + " : " + intent);

Toast.makeText(this, "service call onStartCommand() 메서드",

Toast.LENGTH_SHORT).show();


return START_STICKY;

}


@Override

public void onDestroy() {

// TODO Auto-generated method stub

// super.onDestroy();

mNM.cancel(123);

Toast.makeText(this, "service cal onDestory() 메서드", Toast.LENGTH_SHORT)

.show();

}


@Override

public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

// return null;

Toast.makeText(this, "service cal onBind() 메서드", Toast.LENGTH_SHORT)

.show();

return mBinder;

}


private void showNotification() {

CharSequence text = getText(R.string.local_service_connected);

Notification notification = new Notification(

R.drawable.stat_notify_sync, text, System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

new Intent(this, MainActivity.class), 0);

notification.setLatestEventInfo(this,

getText(R.string.local_service_label), text, contentIntent);

mNM.notify(123, notification);

}

}




반응형

관련글 더보기

댓글 영역