2012-07-13 34 views
5

我想從一個活動中的onCreate發送一個Intent來啓動一個IntentService。然而,IntentService的onHandleIntent從不被接收。我試圖用Intent-Filters改變Manifest,但似乎沒有任何工作。沒有異常被拋出,但IntentService根本不被調用。IntentService不被調用

這裏是的onCreate

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    db = new TwitterDB(this); 
    String[] columns = new String[1]; 
    columns[0] = TwitterDB.TEXT; 
    tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0); 
    tweets = (ListView)findViewById(R.id.listtwitter); 
    tweets.setAdapter(tAdapter); 

    Intent intent = new Intent(this, SyncService.class); 
    intent.putExtra("action", SyncService.TWITTER_SYNC); 
    this.startService(intent); 

} 

這裏是創造者和IntentService類onHandleIntent,我知道它不會被調用,因爲從來沒有logcat中顯示「檢索意圖」。構造函數不叫或者(有在那裏日誌電話,但我刪除它。

public SyncService(){ 
    super("SyncService"); 
    twitterURI = Uri.parse(TWITTER_URI); 
    Log.i("SyncService", "Constructed"); 
} 

@Override 
public void onHandleIntent(Intent i){ 
    int action = i.getIntExtra("action", -1); 
    Log.i("SyncService", "Retrieved intent"); 
    switch (action){ 
     case TWITTER_SYNC: 
      try{ 
       url = new URL(twitterString); 
       conn = (HttpURLConnection)url.openConnection(); 
       syncTwitterDB(); 
       conn.disconnect(); 
      }catch(MalformedURLException e){ 
       Log.w("SyncService", "Twitter URL is no longer valid."); 
       e.printStackTrace(); 
      }catch(IOException e){ 
       Log.w("SyncService", "Twitter connection could not be established."); 
       e.printStackTrace(); 
      } 
      break; 
     default:; 
      // invalid request 
    } 
} 

這裏是的Manifest.xml

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

<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<application android:icon="@drawable/ic_launcher"    
     android:label="@string/app_name" > 
    <activity 
     android:name=".TwitterTwoActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

回答

7

移動你的服務聲明TwitterTwoActivity的XML文件中的範圍之外,因爲我已經做了這裏:

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

<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<application android:icon="@drawable/ic_launcher"    
     android:label="@string/app_name" > 
    <activity 
     android:name=".TwitterTwoActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

</application> 
</manifest> 
7

您需要的路徑定義與服務在您的清單,乾脆把名字是不夠的。

變化

<service android:name="SyncService"/> 

<service android:name=".SyncService"/> 

如果SyncService位於您的軟件包的根目錄中,請在.SyncService之前添加相應的文件夾(如果需要)。

+0

謝謝,這是正確的做法,但它是正確的文件夾,我想 – Jbad26 2012-07-13 19:06:16