首先在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保持平级

view plain copy to clipboard print ?
  1. < service android:name = ".MyService" >
  2. < intent-filter >
  3. < action android:name = "com.kortide.service.MyService" />
  4. </ intent-filter >
  5. </ service >

<service android:name=".MyService"> <intent-filter> <action android:name="com.kortide.service.MyService" /> </intent-filter> </service>

第三,在res目录中创建文件夹raw,添加qingshang.mp3至该文件夹。

第四,在main.xml中添加两个button,位于节点下

view plain copy to clipboard print ?
  1. <Buttonandroid:id= "@+id/btn_start"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="开始服务"
  5. />
  6. <Buttonandroid:id="@+id/btn_stop"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="停止服务"
  10. />
<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中声明该服务。

view plain copy to clipboard print ?
  1. < service android:name = ".ControlMusicService" >
  2. < intent-filter >
  3. < action android:name = "com.kortide.BindService.ControlMusicService" />
  4. </ intent-filter >
  5. </ service >

<service android:name=".ControlMusicService"> <intent-filter> <action android:name="com.kortide.BindService.ControlMusicService" /> </intent-filter> </service>

main.xml中添加两个button。

view plain copy to clipboard print ?
  1. < Button android:id = "@+id/btn_start"
  2. android:layout_width = "wrap_content"
  3. android:layout_height = "wrap_content"
  4. android:text = "开始服务"
  5. />
  6. < Button android:id = "@+id/btn_stop"
  7. android:layout_width = "wrap_content"
  8. android:layout_height = "wrap_content"
  9. android:text = "停止服务"
  10. />
<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;
}

};
}

参考资料

  1. 通过AIDL调用Service

http://www.eoeandroid.com/viewthread.php?tid=1260

http://blog.csdn.net/chenyufei1013/archive/2009/08/17/4455016.aspx

更多相关文章

  1. android 常用代码备份
  2. [Android] 代码实现按钮/图片自旋转(中心旋转)
  3. (20120808)(01)android菜单与对话框--之日期及时间选择对话框
  4. android 的C++代码都加 namespace android
  5. Android代码混淆一定要Export Android Application,否则不起效
  6. Android的Location功能代码
  7. 第一行代码笔记之——Activity启动模式
  8. Android:简单联网获取网页代码
  9. Android 开发笔记 - Android Studio 代码模板

随机推荐

  1. Android(安卓)自定义消息右上角的数字提
  2. Android(安卓)开发进阶之『清除应用中的
  3. Android各大发布市场
  4. Android(安卓)Bluetooth蓝牙技术基础讲解
  5. dex.force.jumbo和Java heap space的问题
  6. android 为摄像头增加闪光灯(s5pv210)
  7. Android获取网速和下载速度
  8. Caused by: java.lang.ClassNotFoundExce
  9. 对于Android的一些牢骚
  10. Android个人所得税计算器