点击播放就崩溃了
MainActivity.java
package com.example.startdemo;
import com.example.startdemo.MyBindService.MyBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
Intent intent1;
Intent intent2;
MyBindService service;
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
// TODO Auto-generated method stub
service = ((MyBinder)binder).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View v) {
switch (v.getId()) {
case R.id.start:
intent1 = new Intent(MainActivity.this, MyStartService.class);
startService(intent1);
break;
case R.id.stop:
stopService(intent1);
break;
case R.id.play:
service.Play();
break;
case R.id.pause:
service.Pause();
break;
case R.id.last:
service.Last();
break;
case R.id.next:
service.Next();
break;
case R.id.bind:
intent2 = new Intent(MainActivity.this, MyBindService.class);
startService(intent2);
bindService(intent2, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.unbind:
unbindService(conn);
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
stopService(intent2);
unbindService(conn);
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyBindService.java
package com.example.startdemo;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyBindService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("info", "Service--onCreate()");
super.onCreate();
}
public class MyBinder extends Binder {
public MyBindService getService() {
return MyBindService.this;
}
}
private void onBind() {
// TODO Auto-generated method stub
Log.i("info", "BindService--onBind()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("info", "BindService--onDestroy()");
super.onDestroy();
}
public void Play() {
// TODO Auto-generated method stub
Log.i("info", "播放");
}
public void Pause() {
// TODO Auto-generated method stub
Log.i("info", "暂停");
}
public void Next() {
// TODO Auto-generated method stub
Log.i("info", "下一首");
}
public void Last() {
// TODO Auto-generated method stub
Log.i("info", "上一首");
}
}