Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';无法在ServiceConnected调用上设置服务触发器_Java_Android_Android Service_Android Service Binding - Fatal编程技术网

Java Can';无法在ServiceConnected调用上设置服务触发器

Java Can';无法在ServiceConnected调用上设置服务触发器,java,android,android-service,android-service-binding,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 Me

我有一个问题,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>

(包装名称为“vdragon.epiplayer”)

它系统地陷入while(!mediamagercommunicator.ready()){try{Thread.sleep(100);}catch(异常e){};部分原因并不明显(我的意思是,我在谷歌搜索“onServiceConnected not Call android”时,尝试了前两页中提出的所有解决方案),但我似乎找不到一个正确的组合:


到目前为止,我所取得的唯一进展是将“BackgroundMediaManager绑定”和“BackgroundMediaManager创建”消息显示在日志上。

您不需要
,而(!mediaManagerCommunicator.connect())
部分,只需调用
connect
一次,它返回什么?您是否在日志中看到:
BackgroundMediaManager绑定
?顺便问一下,调用
context.getApplicationContext()
有什么意义?是的,我明白了。context.getApplicationContext()是我找到的修复程序之一,因为它没有什么区别,所以我就把它放在那里了?所以如果调用了
onBind
,那么也应该调用
onServiceConnected
,如果不调用,尝试调试它,从
onBind
返回一个简单的
Binder
,我刚刚尝试过:服务仍然创建,然后绑定正确,但是仍然没有调用onServiceConnected。您不需要
,而(!mediaManagerCommunicator.connect())
部分,只需调用
connect
一次,它返回什么?您是否在日志中看到:
BackgroundMediaManager绑定
?顺便问一下,调用
context.getApplicationContext()
有什么意义?是的,我明白了。context.getApplicationContext()是我找到的修复程序之一,因为它没有什么区别,所以我就把它放在那里了?所以如果调用了
onBind
,那么也应该调用
onServiceConnected
,如果不调用,试着调试它,从
onBind
返回一个简单的
Binder
,我刚刚试过:服务仍然被创建,然后被正确绑定,但是onServiceConnected仍然没有被调用