绑定到服务故障

时间:2010-12-06 14:35:54

标签: android service

早安全,

我目前正在为Android播放媒体播放器,而我无法将播放器服务绑定到活动并从中提取数据。忍受我......接下来会有很多代码。

我的代码---

接口:

interface MyInterface{       
    void playFile( in String file);
    void shuffle ();
    String getPlayingData();

}

服务:

public class MyService extends Service {

public Context context;
    public static String nowPlayingData = null;

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final MyInterface.Stub mBinder = new MyInterface.Stub() {

    public void playFile(String file) throws DeadObjectException {          
       //Song playing method working great!
    }

    public void shuffle()throws DeadObjectException {       
        //This method picks a random song and passes it to nowPlayingData string                
}

    public String getPlayingData() throws RemoteException {

        return nowPlayingData;
    }
};

@Override
public void onCreate() {
    //Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");
    mp.setLooping(false); // Set looping
    }

@Override
public void onDestroy() {
    //Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    mp.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    //Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

}

}

主屏幕活动,随机播放按钮:

服务在onCreate中启动。当我点击随机播放按钮时,它会使用服务中的方法播放随机歌曲,从而设置nowPlayingData字符串。

public class mainMenu extends Activity {


private MyInterface mInterface;
    private ServiceToken mToken;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Intent startedSvc = new Intent(mainMenu.this, MyService.class);
    boolean success = this.bindService(
            startedSvc, svcConn, Context.BIND_AUTO_CREATE);      
    this.startService(startedSvc);

    findViewById(R.id.shuffle).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


                try {
        sInterface.shuffle();
        } catch (RemoteException e) {
        e.printStackTrace();
        }
        Intent play_screen = new Intent(MyPlayer.getContext(), NowPlaying.class);                   
            startActivity(play_screen);

            }

玩家活动:

我想绑定到服务,并使用接口getPlayingData方法将nowPlayingData拉过来。

public class NowPlaying extends Activity implements OnClickListener {

public static String nowPlayingData = null;
MyInterface mInterface;
boolean isConnected = false;
RemoteServiceConnection conn = null;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.now_playing);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    Log.i("Log", "Player Loaded");

    bindService();
    getData();
    fillView();             
}   


public void fillView(){      
    //This method needs the nowPlayingData string to update the view                
}



class RemoteServiceConnection implements ServiceConnection {
      public void onServiceConnected(ComponentName className, 
     IBinder boundService ) {
mInterface = MyInterface.Stub.asInterface((IBinder)boundService);
isConnected = true;
            Log.d("Now Playing", "Service Connected" );             
      }

      public void onServiceDisconnected(ComponentName className) {
            sInterface = null;
       // updateServiceStatus();
            isConnected = false;
        Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
        getData();
      }
};



private void bindService() {

if(conn == null) {
    conn = new RemoteServiceConnection();
    Intent i = new Intent();
    i.setClassName("com.musicplayer.MyPlayer", "com.musicplayer.MyPlayer.MyService");
    bindService(i, conn, Context.BIND_AUTO_CREATE);
    Log.d( getClass().getSimpleName(), "bindService()" );
 } else {
   Toast.makeText(NowPlaying.this, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show();
 }
}

private void getData() {
 if(conn == null) {
     Log.i("getData", "No Connection");
 } else {
    try {
       String data = mInterface.getPlayingData();  
       Log.i("Data Recieved", data);
       Log.d( getClass().getSimpleName(), "invokeService()" );
    } catch (RemoteException re) {
       Log.e( getClass().getSimpleName(), "RemoteException" );
    }
  }
 }   

}

我一直在网上研究很多例子,也许我的混合尝试导致这种情况无效。我的代码一直用于绑定服务,但从不调用OnServiceConnected,并且conn保持为null。

非常感谢你们提供的任何帮助。

谢谢, 约什

1 个答案:

答案 0 :(得分:1)

Android不会对您的活动进行重入事件调用,因此在onCreate返回之前无法调用onServiceConnected