当前位置:操作系统 > 安卓/Android >>

AIDL(Android Inte易做图ce definition language)学习笔记一

一、源码结构:

\

二、Manifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.qiao.aidl.test1" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="7" /> 
     
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
     
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:label="@string/app_name" 
            android:name=".AIDL_Demo_1Activity" > 
            <intent-filter > 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <service android:name="com.qiao.aidl.service.MusicService" android:enabled="true"></service> 
    </application> 
 
</manifest> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qiao.aidl.test1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
 
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AIDL_Demo_1Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.qiao.aidl.service.MusicService" android:enabled="true"></service>
    </application>

</manifest>

 

三、AIDL文件

package com.qiao.aidl.aidl; 
 
inte易做图ce IServicePlayer{ 
    void play(); 
    void pause(); 
    void stop(); 
    int getDuration(); 
    int getCurrentPosition(); 
    void seekTo(int current); 
    boolean setLoop(boolean loop); 

package com.qiao.aidl.aidl;

inte易做图ce IServicePlayer{
 void play();
 void pause();
 void stop();
 int getDuration();
 int getCurrentPosition();
 void seekTo(int current);
 boolean setLoop(boolean loop);
}


四、MusicService

package com.qiao.aidl.service; 
 
import java.io.File; 
import java.io.IOException; 
 
import com.qiao.aidl.aidl.IServicePlayer; 
import com.qiao.aidl.test1.R; 
 
import android.app.Service; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Environment; 
import android.os.IBinder; 
import android.os.RemoteException; 
 
public class MusicService extends Service{ 
     
    public static final String TAG = "MusicService"; 
     
    public static MediaPlayer mediaPlayer = null; 
     
    public boolean isPause = false; 
     
     
    @Override 
    public void onCreate() { 
        // TODO Auto-generated method stub  
        super.onCreate(); 
        //第一种加载音乐文件的方法,即把音乐文件放在res资源文件中  
//      mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.test);  
        //第二种加载音乐文件的方法,读取本地SD卡上的音乐文件  
        mediaPlayer = new MediaPlayer(); 
        try { 
            loadMusicFileFromSDCard("光辉岁月_Beyond.mp3"); 
        } catch (IOException e) { 
            System.out.println("文件读取失败"); 
            e.printStackTrace(); 
        } 
    } 
     
    private void loadMusicFileFromSDCard(String fileName) throws IOException{ 
        File audioFile = new File("/sdcard/DUOMI

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,