首先在eclipse中創建Android工程TestService和TestActivity.java,同時創建服務類MyService
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
}
其次,在AndroidManifest.xml中聲明該服務,注意和activity保持平級
- < service android:name = ".MyService" >
- < intent-filter >
- < action android:name = "com.kortide.service.MyService" />
- </ intent-filter >
- </ service >
第三,在res目錄中創建文件夾raw,添加qingshang.mp3至該文件夾。
第四,在main.xml中添加兩個button,位於節點下
- <Button android:id= "@+id/btn_start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="開始服務"
- />
- <Button android:id="@+id/btn_stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="停止服務"
- />
第五,在TestActivity.java中的onCreate回調中添加代碼
public class TestActivity extends Activity {
public String MYSERVICE = "com.kortide.service.MyService";
public String CONTROLMUSICSERVICE = "com.kortide.service.ControlMusicService";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 創建2個按鈕控制服務
Button startbtn = (Button) findViewById(R.id.btn_start);
Button stopbtn = (Button) findViewById(R.id.btn_stop);
startbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 啓動服務,通過Intent的方法來啓動
Intent serviceIntent = new Intent();
serviceIntent.setAction(MYSERVICE);
startService(serviceIntent);
}
});
stopbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent();
serviceIntent.setAction(MYSERVICE);
stopService(serviceIntent);
}
});
Toast.makeText(this, "TestActivity created...", Toast.LENGTH_LONG).show();
Log.i("CHEN", "TestActivity created...");
}
}
第六,選擇菜單Run->Run,選擇“Android Application”查看運行結果
- 通過ADIL調用服務
首先,創建Android工程BindService和BindService.java,創建IMusicControlService.aidl(可以使用菜單File->New->File來創建),elipse會在gen中自動創建一些代碼
interface IMusicControlService
{
void playMusic();
void stopMusic();
}
其次,同上面的示例,在res目錄中創建文件夾raw,添加qingshang.mp3至該文件夾。AndroidManifest.xml中聲明該服務。
- < service android:name = ".ControlMusicService" >
- < intent-filter >
- < action android:name = "com.kortide.BindService.ControlMusicService" />
- </ intent-filter >
- </ service >
main.xml中添加兩個button。
- < Button android:id = "@+id/btn_start"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:text = "開始服務"
- />
- < Button android:id = "@+id/btn_stop"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:text = "停止服務"
- />
第三,創建類ControlMusicService
public class ControlMusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public MediaPlayer mplayer;
private final IMusicControlService.Stub binder = new IMusicControlService.Stub() {
MediaPlayer player;
@Override
public void playMusic() throws RemoteException {
Log.i("CHEN", "Play music...");
player = MediaPlayer.create(ControlMusicService.this,
R.raw.qingshang);
player.start();
}
@Override
public void stopMusic() throws RemoteException {
Log.i("CHEN", "Stop music...");
if (player.isPlaying()) {
player.stop();
}
}
};
}
在BindService.java中添加綁定服務的代碼
public class BindService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setClass(this,ControlMusicService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
Log.i("CHEN", "Bind service...");
Button btnplay = (Button)findViewById(R.id.btn_play);
Button btnstop = (Button)findViewById(R.id.btn_stop);
btnplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
iMusicControlService.playMusic();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnstop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
iMusicControlService.stopMusic();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public IMusicControlService iMusicControlService;
private final ServiceConnection serviceConnection = new ServiceConnection() {
// 第一次連接service時會調用這個方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMusicControlService = IMusicControlService.Stub
.asInterface(service);
}
// service斷開的時候會調用這個方法
@Override
public void onServiceDisconnected(ComponentName name) {
iMusicControlService = null;
}
};
}
參考資料
- 通過AIDL調用Service
http://www.eoeandroid.com/viewthread.php?tid=1260
http://blog.csdn.net/chenyufei1013/archive/2009/08/17/4455016.aspx