안드로이드 개발 정보
(글 수 1,069)
참고
test service
test activity
package lowmans.MyServiceOnBindTest;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyServiceOnBindTest extends Activity implements OnClickListener{private final String tag = "MyServiceOnBindTest";
private LinearLayout mLayout;
private Button mStart;
private Button mStop;
private final int mStartID = 0;
private final int mStopID = 1;
private MyServiceOnBind mService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
LayoutParams mParams = new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mStart = new Button(this);
mStart.setLayoutParams(mParams);
mStart.setId(mStartID);
mStart.setText("ServiceOnBind");mStart.setOnClickListener(this);
mStop = new Button(this);
mStop.setLayoutParams(mParams);
mStop.setId(mStopID);
mStop.setText("SetviceUnBind");mStop.setOnClickListener(this);
mLayout.addView(mStart);
mLayout.addView(mStop);
setContentView(mLayout);
}
private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) {mService = ((MyServiceOnBind.MyBinder)service).getService();
mService.test(3);
}
public void onServiceDisconnected(ComponentName className) {mService = null;
}
};
public void onClick(View v){ switch(v.getId()){case mStartID:
Log.i(tag , "Start Button OnClick");
bindService(new Intent("lowmans.MyServiceOnBindTest.MyServiceOnBind") , mConnection , Context.BIND_AUTO_CREATE);
break;
case mStopID:
Log.i(tag , "Stop Button OnClick");
break;
}
}
}
package lowmans.MyServiceOnBindTest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyServiceOnBind extends Service {final String tag = "MyServiceOnBind";
private final IBinder binder = new MyBinder();
public void onCreate(){Log.i(tag , "OnCreate");
}
/*
public void onStart(Intent intent , int startId){Log.i(tag , "onStart");
}
*/
public void onDestroy(){Log.i(tag , "onDestroy");
}
public IBinder onBind(Intent intent){Log.i(tag , "onBind");
return binder;
}
public class MyBinder extends Binder{ MyServiceOnBind getService(){return MyServiceOnBind.this;
}
}
public void test(int x){Log.i(tag , "test : " + x);
}
}



