2014-12-03 15 views
0

我有一個應用程序必須綁定到另一個包中的服務。我已經放置了一個自定義意圖過濾器以使其工作。可悲的是,應用程序不會綁定。日誌說它找不到服務。應用程序不會綁定到服務

申請A是在包「com.example.app_a」 該服務是在另一個包「com.example.app_talker_service」

所以我不能指服務與xxx.class解決方案,所以我的猜測是在服務包的清單文件中使用intent過濾器。

另一方面,應用程序A將需要對服務進行綁定以使其啓動(如果它尚未啓動),並且稍後它將通過使用廣播接收器與它通信。我做了一些實驗,我注意到廣播工作正常,但是什麼是wrond是由於某種原因,應用程序A似乎無法在綁定過程中找到我的服務....

這裏是結合處理通信(應用程序A):

@Override 
protected void onStart() 
{ 
    // TODO Auto-generated method stub 
    super.onStart(); 

    //Bind to service 
    getApplicationContext().bindService(new Intent("com.example.talker_service.SERVICE"), mConnection,Context.BIND_AUTO_CREATE); 


} 
    private boolean mIsBound = false; 

private ServiceConnection mConnection = new ServiceConnection() 
{ 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     // TODO Auto-generated method stub 
     mIsBound = true; 
     Toast.makeText(getApplicationContext(), "CONNECTED TO SERVICE!", Toast.LENGTH_SHORT).show(); 

     Intent intent = new Intent(); 
     intent.setAction("com.example.talker_service.SERVICE"); 
     intent.putExtra("REQUEST", "REGISTER APP"); 

     intent.putExtra("FILTER", "com.example.app_a"); 
     intent.putExtra("NAME", "Applicazione A"); 

     String[] components = {"NUMBER_SENT","CHANGE_TEXT_COLOR","CHANGE_TEXTVIEW_SIZE"}; 
     intent.putExtra("COMPONENTS", components); 

     MainActivityA.this.sendBroadcast(intent); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     // TODO Auto-generated method stub 
     mIsBound = false; 
    } 

}; 

這裏反而是我把它叫做Talker_service服務的cmanifest:

en<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.app_talker_service" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <service 
      android:name=".Talker_Service" 
      android:enabled="true" 
      android:exported="true" 
      android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" > 
      <intent-filter > 
       <action android:name="com.example.talker_service.SERVICE"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
       <data android:mimeType="text/plain" /> 
      </intent-filter> 
     </service> 

     <activity 
      android:name=".ConnectionManagerActivity" 
      android:label="@string/title_activity_connection_manager" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我不undertsand爲什麼它不綁定..我把意向過濾器,我錯過了什麼?嗯,和日誌這樣說:

12-03 22:45:13.786:W/ContextImpl(26076):與startService隱含的意圖是 不安全:意向{ 行爲= com.example.talker_service。 SERVICE} android.content.ContextWrapper.bindService:517 com.example.app_a.MainActivityA.onStart:81 android.app.Instrumentation.callActivityOnStart:1171 12-03 22:45:13.786:W/ActivityManager(764) :無法啓動服務意圖{>行爲= com.example.talker_service.SERVICE} U = 0:未發現

這反而是對應用程序的清單文件中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.app_a" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="19" /> 
    <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" ></uses-permission> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivityA" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

回答

0

爲什麼使用意圖過濾器不工作,我不知道(它說,它不能找到它),但通過使用下面的代碼我能讓我的應用程序連接到遠程服務:

Intent i = new Intent(); 
    i.setClassName("com.example.app_talker_service", "com.example.app_talker_service.Talker_Service"); 
    bindService(i, conn, Context.BIND_AUTO_CREATE); 

所以使用seClassName方法,並提供了pacake和服務是全路徑的名字,我就能夠使它發揮作用。我不確定這是否屬於這類問題的「最佳實踐」領域,但對於我的工作而言。任何解決方案都比沒有解決方案好。我發現此解決方案適用於此鏈接:

Remote Service Tutorial

相關問題