为了账号安全,请及时绑定邮箱和手机立即绑定

点击播放就崩溃了

  • 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", "上一首");

}

}










http://img1.sycdn.imooc.com//578742d500012e7b13000548.jpg

正在回答

1 回答

onbind()方法没有返回Binder子类的对象。

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return new MyBinder();

}


0 回复 有任何疑惑可以回复我~
#1

onceeeeday 提问者

谢谢你!
2016-07-16 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第3季)
  • 参与学习       74912    人
  • 解答问题       420    个

手势识别、数据存储和4大组件,让你牢牢掌握之前的基础知识点

进入课程

点击播放就崩溃了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信