1. Unbound Service (or Started Service)
startService(); 로 호출
이러한 서비스는 작업 결과를 돌려주기 위해서는 BroadcastReceiver를 만들어서 결과를 받아야 한다.
그 Receiver는 Activity 와 상호 작용하여 UI를 업데이트 해야 한다.
2. Bound Service
bindService(); 로 호출
Unbound Service와 달리 Activity와 즉각 상호작용이 가능하다.
p.s 간단하게 썼지만 BroadcastReceiver 와 Activity간의 상호작용은 꽤나 어렵다. 다음의 예제에서 답을 찾을 수 있다.
package com.example.usecalcproject;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.calcproject.ICalc;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText mEditExpr1;
private Button mBtnCalc1;
private TextView mtxtResult1;
private EditText mEditExpr2;
private Button mBtnCalc2;
private TextView mtxtResult2;
private BroadcastReceiver mBR;
/* 옵션 구현(1/5) */
ICalc mCalc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditExpr1 = (EditText) findViewById(R.id.editExpr1);
mBtnCalc1 = (Button) findViewById(R.id.btnCalc1);
mBtnCalc1.setOnClickListener(this);
mtxtResult1 = (TextView) findViewById(R.id.txtResult1);
mEditExpr2 = (EditText) findViewById(R.id.editExpr2);
mBtnCalc2 = (Button) findViewById(R.id.btnCalc2);
mBtnCalc2.setOnClickListener(this);
mtxtResult2 = (TextView) findViewById(R.id.txtResult2);
if (savedInstanceState != null) {
String expr1 = savedInstanceState.getString("expr1", "");
mEditExpr1.setText(expr1);
String result1 = savedInstanceState.getString("result1", "");
mtxtResult1.setText(result1);
String expr2 = savedInstanceState.getString("expr2", "");
mEditExpr2.setText(expr2);
String result2 = savedInstanceState.getString("result2", "");
mtxtResult2.setText(result2);
}
mBR = new MyReceiver();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("expr1", mEditExpr1.getText().toString());
outState.putString("result1", mtxtResult1.getText().toString());
outState.putString("expr2", mEditExpr2.getText().toString());
outState.putString("result2", mtxtResult2.getText().toString());
}
// Inner class
// Receiver가 Activity에 접근하는 하나의 방법
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String result = intent.getStringExtra("result");
mtxtResult1.setText(result);
}
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("example.mytest.CALCRESULT");
registerReceiver(mBR, filter);
/* 옵션 구현(2/5) */
Intent i = new Intent();
i.setClassName("com.example.calcproject", "com.example.calcproject.CalcService");
bindService(i, srvConn, BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBR);
/* 옵션 구현(3/5) */
unbindService(srvConn);
}
/* 옵션 구현(4/5) */
ServiceConnection srvConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
mCalc = ICalc.Stub.asInterface(binder);
}
@Override
public void onServiceDisconnected(ComponentName className) {
mCalc = null;
}
};
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCalc1:
if (mEditExpr1.getText().length() > 0) {
Intent i = new Intent();
i.setClassName("com.example.calcproject", "com.example.calcproject.CalcService");
i.putExtra("expr", mEditExpr1.getText().toString());
startService(i);
}
break;
case R.id.btnCalc2: /* 옵션 구현(5/5) */
if (mEditExpr2.getText().length() > 0) {
try {
String result = mCalc.calc(mEditExpr2.getText().toString());
mtxtResult2.setText(result);
}
catch (RemoteException e) {
e.printStackTrace(); // Security risk!
}
}
break;
}
}
}
해결 방법 중 하나인 AIDL 파일
package com.example.calcproject;
interface ICalc {
String calc(String expr);
}
댓글 없음:
댓글 쓰기