AIDL和远程Service调用
2、在Service中建个子类实现这接口的存根(stub)对象
3、并在onBind()方法中返回这个存根对象。
4、在Activity中使用绑定服务的方式连接Service,但是不用Intent来传递信息,而是在ServiceConnection的onServiceConnected方法里,获得Service中Stub对象的客户端使用代理。我们通过操作Activity中的代理就可以达到操作Service中的MediaPlayer对象的目的。这样我们就可以想用本地对象一样操作Service中的对象了,那么进度条一类的需求自然也就迎刃而解。
<?xml version="1.0" encoding="utf-8"?>
<manifest package="app.android.elfplayer" xmlns:android="http://schemas.android.com/apk/res/android" android:versioncode="1" android:versionname="1.0">
<uses -sdk="" android:minsdkversion="7">
<uses -permission="" android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses>
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".CoverActivity">
<intent -filter="">
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent>
</activity>
<activity android:name=".PlayerActivity">
</activity>
<service android:name=".MusicService" android:enabled="true">
</service>
</application>
</uses></manifest>
package app.android.elfplayer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class CoverActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cover);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Intent mainIntent = new Intent(CoverActivity.this,PlayerActivity.class);
CoverActivity.this.startActivity(mainIntent);
CoverActivity.this.finish();
}
}, 2000);
}
}
package app.android.elfplayer;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class PlayerActivity extends Activity {
public static final int PLAY = 1;
public static final int PAUSE = 2;
ImageButton imageButtonFavorite;
ImageButton imageButtonNext;
ImageButton imageButtonPlay;
ImageButton imageButtonPre;
ImageButton imageButtonRepeat;
SeekBar musicSeekBar;
IServicePlayer iPlayer;
boolean isPlaying = false;
boolean isLoop = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
imageButtonFavorite = (ImageButton) findViewById(R.id.imageButtonFavorite);
imageButtonNext = (ImageButton) findViewById(R.id.imageButtonNext);
imageButtonPlay = (ImageButton) findViewById(R.id.imageButtonPlay);
imageButtonPre = (ImageButton) findViewById(R.id.imageButtonPre);
imageButtonRepeat = (ImageButton) findViewById(R.id.imageButtonRepeat);
musicSeekBar = (SeekBar) findViewById(R.id.musicSeekBar);
bindService(new Intent(PlayerActivity.this, MusicService.class), conn, Context.BIND_AUTO_CREATE);
startService(new Intent(PlayerActivity.this, MusicService.class));
imageButtonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("yao", "imageButtonPlay -> onClick");
if (!isPlaying) {
try {
iPlayer.play();
} catch (RemoteException e) {
e.printStackTrace();
}
imageButtonPlay.setBackgroundResource(R.drawable.pause_button);
isPlaying = true;
} else {
try {
iPlayer.pause();
} catch (RemoteException e) {
e.printStackTrace();
}
imageButtonPlay.setBackgroundResource(R.drawable.play_button);
isPlaying = false;
}
}