根據這article我已經實現了活動,發送字符串服務。你可以看看下面,也許它會有所幫助。
MainActivity.java
package com.example.androidtranslator;
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.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private Messenger mService = null;
private boolean mBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some gui code etc...
/**
* Connect to an application service, creating it if needed
*/
bindService(new Intent(this, TranslatorService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/**
* on some GUI action (click button) send message
*
* @param view
*/
public void translate(View view) {
if (!mBound)
return;
Message msg = Message.obtain(null, TranslatorService.MSG_STRING,
"Some message (it can be from textView for example)");
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@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;
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
mBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = new Messenger(service);
mBound = true;
}
};
}
TranslatorService.java
package com.example.androidtranslator;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;
public class TranslatorService extends Service {
public static final int MSG_STRING = 0;
/**
* Handler of incoming messages from clients.
* Show Toast with received string
*/
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_STRING:
Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
}
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
final Messenger mMessenger = new Messenger(new IncomingHandler());
/**
* When binding to the service, we return an interface to our messenger
* for sending messages to the service.
*/
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}
AIDL是不與'Service'溝通的唯一途徑,而這絕不是最容易的。 –
這個答案不正確,你可以綁定到一個服務進行通信,或者你可以發送intents。看看[Android Developers](http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService)文檔。 –