2014-11-03 125 views
0

如果一個應用程序在Android清單,這意味着我可以在另一個應用程序正確運行的服務的導出的服務?例如,服務在清單文件中看起來像這樣。運行ContactsSyncAdapterService從外部Android應用

<service 
      android:name=".account.contactsync.ContactsSyncAdapterService" 
      android:exported="true" 
      android:process=":contacts" > 
      <intent-filter> 
       <action android:name="android.content.SyncAdapter" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.content.SyncAdapter" 
       android:resource="@xml/sync_contacts" /> 
      <meta-data 
       android:name="android.provider.CONTACTS_STRUCTURE" 
       android:resource="@xml/contacts" /> 
     </service> 

正如你可以看到服務被設置爲機器人:出口=「真」這意味着我應該能夠從應用程序外面跑呢?如何做到這一點

我嘗試這樣做,也沒有工作

final Intent intent = new Intent(); 

        ComponentName cName = new ComponentName 
          ("com.myapp","com.myapp.ContactsSyncAdapterService"); 

        intent.setComponent(cName); 
        startActivity(intent); 

回答

0

你錯過了你的組件名稱的.account.contactsync

ComponentName cName = new ComponentName("com.myapp", 
    "com.myapp.account.contactsync.ContactsSyncAdapterService"); 

而且你需要使用startService()而不是startActivity() - startActivity()只開始活動。

+0

嗨,謝謝你的迴應。但它仍然無法運行同步服務。我想通過從應用以外運行該服務來讓應用刷新其聯繫人。它似乎還沒有迴應它 – 2014-11-03 23:27:14

+0

我假設你已經閱讀[運行同步適配器培訓](http://developer.android.com/training/sync-adapters/running-sync-adapter.html)和評估了啓動同步適配器的每種方法? – ianhanniballake 2014-11-03 23:31:47

+0

是的,我有。它實際上爲這個應用程序[鏈接](https://github.com/mar-v-in/XMPP-for-Android/blob/master/AndroidManifest.xml)同步適配器 – 2014-11-03 23:38:05