2012-07-13 20 views
5

IntentServiceを開始するアクティビティでonCreateからIntentを送信しようとしています。しかし、IntentServiceのonHandleIntentは決して受信されません。私はインテントフィルターでマニフェストを変更しようとしましたが、何も動作していないようです。例外はスローされませんが、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
私はここに行ったように、XMLファイルにTwitterTwoActivityの範囲の外にあなたのサービスの宣言を移動し

<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

関連する問題