Android远程服务执行但onStartCommand从未调用过

时间:2012-11-08 00:44:57

标签: android service aidl

因此,经过2-3个小时的失败后,我终于让我的远程服务以某种方式工作,但它的表现真的很奇怪。

我正在使用AIDL将mediaPath字符串发送到我的服务,它开始播放音乐就好了,但是onStartCommand永远不会被调用,而且Apps中的服务条目没有我在清单文件中设置的标签/描述。 [http://i50.tinypic.com/344p349.png]

如果我终止主Activity进程,服务也会终止,尽管它位于一个单独的进程中。这是怎么回事? [http://i49.tinypic.com/16hpa86.png]

我从未得到“服务已断开连接”日志,这应该在服务从“活动”中解除绑定时发生。

服务代码:

package com.example.randomserviceshitnot;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MojPrviServis extends Service {
    private final Servis.Stub binder = new Servis.Stub() {
        public void execute(String mediaPath) throws RemoteException {
            MediaPlayer mp = new MediaPlayer();

            try {
                mp.setDataSource(mediaPath);
                mp.prepare();
            } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) {
                e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace();
            } catch (IOException e) { e.printStackTrace(); }
                mp.start();
        }
    };

    public void onCreate() {
        super.onCreate();
        Log.d("Filip", "Service onCreate called.");
    }

    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Filip ", "Service onStart called.");
        return START_STICKY;
    }
}

活动代码:

package com.example.randomserviceshitnot;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MainActivity extends Activity {
    private Servis mBoundService;
    private boolean mIsBound = false;
    private static final String mediaPath = Environment.getExternalStorageDirectory().toString()+"/Music/Art Of The Dress(Archie Remix).mp3";

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("Filip ", "Service connected."); 
            mBoundService = Servis.Stub.asInterface(service);
            try {
                mBoundService.execute(mediaPath);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            Log.d("Filip ", "Service disconnected.");
            mBoundService = null;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        doBindService();
    }

    void doBindService() {
        Intent s = new Intent();
        s.setAction("remote.servis");
        bindService(s, mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }

    public void onPause() {
        super.onPause();
        doUnbindService();
    }

    void doUnbindService() {
        if(mIsBound) {
            unbindService(mConnection);
            mIsBound=false;
        }
    }
}

AIDL:

package com.example.randomserviceshitnot;

interface Servis {
    void execute(String s);
}

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.randomserviceshitnot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MojPrviServis"
            android:label="@string/servis_koji_notifikuje"
            android:description="@string/proces_desc"
            android:icon="@drawable/ic_launcher"
            android:process=":dep" >
            <intent-filter>
                <action android:name="remote.servis" />
            </intent-filter>
        </service>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

http://developer.android.com/guide/components/services.html

阅读onStartCommand()和onBind()

的文档

当你调用bindService()onStartCommand()时,不会调用它。但是onBind()被调用。

如果服务是使用bindservice()进行的,所有客户端都会退出,服务也会终止。

要播放音乐,您应该使用startService()而不是bind。如果以这种方式启动服务,那么当客户端存在时它不会停止。它必须在完成后自行停止。

相关问题