通过AIDL调用Service

阅读更多
在网上找的一些关于service的例子都比较简单,都是通过startService("action")启动service,然后通过stopService("service")停止service。只能启动和停止service没有发挥service的功能。下面我通过介绍关于AIDL启动service来控制音乐播放的例子来说明通过前台控制service的使用。
1.在工程的包中一个后缀为aidl的文件
IMusicControlService.aidl

package com.dream.androidstud2.service;--------包名一定要和当前工程的包名一样哦!

interface IMusicControlService
{
void playMusic(); -------->播放音乐
void stopMusic(); ------->停止播放音乐
}

点击保存后,在 gen/上述包名的目录下就创建了一个IMusicControlService.java文件了
2.在res/layout目录下创建布局文件:
startserviceactivity.xml

androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="18px"/>

你可能感兴趣的:(Android,音乐,XML)