2012-11-08 37 views
1

因此,經過2-3小時的失敗,我終於讓遠程服務以某種方式工作,但它的行爲真的很奇怪。Android遠程服務執行,但onStartCommand從未呼籲

我使用AIDL發送mediaPath字符串到我的服務,它開始播放音樂就好了,但onStartCommand永遠不會被調用,並且應用中的服務條目沒有我在清單文件中設置的標籤/描述。 [http://i50.tinypic.com/344p349.png]

同樣,如果我終止主Activity活動進程,服務終止,儘管它駐留在一個單獨的進程中。這是應該如何? [http://i49.tinypic.com/16hpa86.png]

我從來沒有得到「服務斷開」日誌,這應該發生在服務從Activity中解除綁定時發生。

服務代碼:

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> 
+0

我自己也有同樣的問題。 –

回答

0

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

閱讀onStartCommand()和onBind()

當你調用bindService()onStartCommand()不叫的文檔。但是onBind()被調用。

此外,如果所有客戶端都退出,並且服務被綁定到bindservice(),則該服務終止。

對於播放音樂,您應該使用startService()而不是綁定。如果服務以這種方式啓動,那麼當客戶端存在時它不會停止。它完成後必須自行停止。