首先在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. 手机壁纸设置相关
  2. 问题:plugin with id 'android' not found
  3. 2011.09.09(2)——— android 桌面添加快捷方式
  4. Android环境配置
  5. android Activity单元测试
  6. Android(安卓)ListView+image的使用
  7. android 单元测试
  8. 保存BitMap,File到本地
  9. Android:创建快捷方式

随机推荐

  1. SQL Server:触发器实例详解
  2. Oracle 删除用户和表空间详细介绍
  3. SQLServer数据库从高版本降级到低版本实
  4. Ubuntu 下安装SQL Server教程
  5. SQL判断语句用法和多表查询
  6. SQLServer获取临时表所有列名或是否存在
  7. Sql Server中Substring函数的用法实例解
  8. Mysql数据库性能优化三(分表、增量备份、
  9. SQL where条件和jion on条件的详解及区
  10. SQL 多条件查询几种实现方法详细介绍