我正在练习服务,我必须创建下一个简单的应用程序: 在主要活动中,我有2个edittext在每个应用程序中插入一个数字。然后我有5个按钮,每个按钮指的是一个物理操作(加,减,乘,除和因子)。选择一项服务时,服务必须开始处理所选操作并将结果返回给用户界面。
我正在阅读很多关于服务的教程,并尝试自己实现。我正在使用信使将数据从服务返回到UI,但不确定它是否正常。要将数据发送到服务我正在使用Intent的putExtra,但同样,不确定这是否是正确的方法。
除此之外,我还怀疑如何根据按下的按钮告诉服务哪个操作必须执行(加,减,乘,除和因子)。
这是我的主要活动代码:
public class MainActivity extends Activity {
private static class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CalcService.MSG_SET_RESULT:
//...
break;
default:
super.handleMessage(msg);
break;
}
}
};
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ResultBinder binder = (ResultBinder) service;
mService = binder.getService();
mHandler = new IncomingHandler();
mMessenger = new Messenger(mHandler);
mService.setMessenger(mMessenger);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
bnSuma.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMessageToService();
}
});
}
private void sendMessageToService() {
String Val1 = mVal1.getText().toString();
String Val2 = mVal2.getText().toString();
try {
Valor1 = Integer.parseInt(Val1);
Valor2 = Integer.parseInt(Val2);
}catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Insert a numeric value", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(MainActivity.this, CalcService.class);
intent.putExtra(CalcService.EXTRA_VAL1, Valor1);
intent.putExtra(CalcService.EXTRA_VAL2, Valor2);
startService(intent);
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(MainActivity.this, CalcService.class);
bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(mServiceConnection);
}
}
这是我的服务类。还没有实现相关的功能。
public class CalcService extends Service {
public static final int MSG_SET_RESULT = 1;
public static final String EXTRA_VAL1 = "extra_val1";
public static final String EXTRA_VAL2 = "extra_val2";
private int mResult;
private Messenger mMessenger;
public class ResultBinder extends Binder {
CalcService getService() {
return CalcService.this;
}
}
private final ResultBinder mResultBinder = new ResultBinder();
private class ResultThread extends Thread{
@Override
public void run() {
//...
try {
Message msg = Message.obtain(null, MSG_SET_RESULT);
msg.arg1 = mResult;
mMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
stopSelf();
}
};
private ResultThread mResultThread;;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mResultThread = new ResultThread();
mResultThread.start();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mResultBinder;
}
public void setMessenger(Messenger messenger){
mMessenger = messenger;
}
@Override
public void onDestroy() {
super.onDestroy();
mResultThread = null;
}
}