简体   繁体   English

Android:服务不会绑定

[英]Android: Service won't bind

Im trying to create bound service. 我正在尝试创建绑定服务。 As a test I created service which plays music: 作为测试,我创建了播放音乐的服务:

public class MusicService extends Service {
    private final IBinder myBinder = new LocalBinder();
    MediaPlayer player;

    @Override
    public IBinder onBind(Intent arg0) {
        return myBinder;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this,R.raw.teardrop);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);
        player.start();
    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    public class LocalBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }
    }    
}

And when I bind it from Activity nothing happens: 当我从Activity绑定它时,什么也没发生:

public class MainActivity extends TabSwipeActivity {
    boolean isBound = false;
    MusicService myService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //Some code        
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
        if(isBound){
            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "bind failed", Toast.LENGTH_SHORT).show();
        }
    }
    private ServiceConnection myConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            myService = binder.getService();
            isBound = true;
        }       
        public void onServiceDisconnected(ComponentName arg0) {
            isBound = false;
        }
    };    
}

Service is registred in manifest: 服务已在清单中注册:

<service android:name=".MusicService" />

Bind failed appears and nothing happens 绑定失败出现,没有任何反应

EDIT: bindService() returns false 编辑:bindService()返回false

EDIT2: when I add complete name in manifest eg. EDIT2:当我在清单中添加完整名称时 com.mypackage.mypackage2.MusicService bind Service() returned true. com.mypackage.mypackage2.MusicService绑定Service()返回true。 But onServiceConnected() is never called. 但是永远不会调用onServiceConnected()。

Next question is: When I create service which implements LocationListener, what should I use to send message to activity everytime when onLocationChanged()? 下一个问题是:当我创建实现LocationListener的服务时,每次onLocationChanged()时应该使用什么向活动发送消息?

I already know the solution. 我已经知道了解决方案。 I extends TabActivity made by actionBarSherlock instead of Activity . 我扩展了actionBarSherlock而不是Activity制成的TabActivity This is known issue: 这是已知问题:

 getApplicationContext().bindService();

fix that. 解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM