无法管理服务触发onServiceConnected调用

时间:2016-04-30 22:35:07

标签: java android android-service android-service-binding

我有这个问题,没有调用onServiceConnected方法,我尝试了多个解决方案但没有一个工作...

我的服务管理代码归结为:

public class BackgroundMediaManagerCommunicator {
private Messenger   service = null;
private boolean     isBound = false;
private Context     context;
final Messenger messenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case PLAYLIST:
                playlist = playlist.getClass().cast(msg.obj);
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder binder) {
        service = new Messenger(binder);
        android.util.Log.v(TAG, "BackgroundMediaManagerCommunicator: Attached service");
        isBound = true;
    }
    @Override
    public void onServiceDisconnected(ComponentName className) {
        isBound = false;
        service = null;
        android.util.Log.d(TAG, "BackgroundMediaManagerCommunicator: Unexpected disconnection, Trying to reconnect");
    }
};

public BackgroundMediaManagerCommunicator(Context context)
{
    this.context = context;
}

public boolean connect()
{
    return context.getApplicationContext().bindService(new Intent(context.getApplicationContext(), BackgroundMediaManager.class), connection, Context.BIND_AUTO_CREATE);
}

public void disconnect() {
    if (isBound) {
        context.unbindService(connection);
        isBound = false;
    }
}

public boolean ready()
{
    return isBound;
}

和我的服务:

public class BackgroundMediaManager extends Service {
final Messenger messenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case PLAY:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received Play");
                play(playlist.get(msg.arg1));
                idPlaying = msg.arg1;
                break;
            case SET_PLAYLIST:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received Set playlist");
                playlist = playlist.getClass().cast(msg.obj);
                break;
            case GET_PLAYLIST:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received Get playlist");
                try {
                    msg.replyTo.send(Message.obtain(null, BackgroundMediaManagerCommunicator.PLAYLIST, (Object)playlist));
                } catch (RemoteException e)
                {
                    android.util.Log.d(TAG, "BackgroundMediaManager: severe case of ded client");
                }
                break;
            case SET_VIEW:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received set view");
                setDisplay((SurfaceHolder)msg.obj);
                break;
            case PAUSE:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received pause");
                if (mediaPlayer.isPlaying())
                    mediaPlayer.pause();
                else
                    mediaPlayer.start();
                break;
            case NEXT:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received next");
                move(1);
                break;
            case PREVIOUS:
                android.util.Log.v(TAG, "BackgroundMediaManager: Received previous");
                move(-1);
                break;
            default:
                super.handleMessage(msg);
        }
    }
};

public BackgroundMediaManager()
{
    super();
}

@Override
public IBinder onBind(Intent intent) {
    android.util.Log.v(TAG, "BackgroundMediaManager Bound");
    return messenger.getBinder();
}
@Override
public void onCreate() {
    startCommand();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startCommand();
    return START_STICKY;
}
private void startCommand()
{
    android.util.Log.v(TAG, "BackgroundMediaManager created");
    mediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChanged);
}
@Override
public void onDestroy()
{
    super.onDestroy();
}

从主要活动中这样称呼:

if (!mediaManagerCommunicator.connect())
        startService(new android.content.Intent(this, BackgroundMediaManager.class));
    while (!mediaManagerCommunicator.connect()) { try { Thread.sleep(100); } catch (Exception e){} };
    outOfCreateReadying.postDelayed(new Runnable() {
        @Override
        public void run() {
            while (!mediaManagerCommunicator.ready()) { try { Thread.sleep(100); } catch (Exception e){} };
        }

    }, 100);

和我关于服务的清单:

<service
        android:enabled="true"
        android:name="vdragon.epiplayer.BackgroundMediaManager"
        android:process=":remote">
    </service>

(pkg名称是&#39; vdragon.epiplayer&#39;)

系统地陷入困境(!mediaManagerCommunicator.ready()){try {Thread.sleep(100); } catch(例外e){}};部分原因没有明显的原因(我的意思是我尝试了两个第一页上提出的所有解决方案,当谷歌搜索&on;服务连接没有被称为android&#39;),我似乎无法找到一个有效的组合正确的:

到目前为止,我所取得的唯一进展是收到消息&#34; BackgroundMediaManager Bound&#34;和&#34; BackgroundMediaManager创建&#34;显示在日志上。

0 个答案:

没有答案