2012-07-13 12 views
5

Próbuję wysłać Intent z onCreate w działaniu, aby uruchomić IntentService. Jednak onHandleIntent IntentService nigdy nie jest odbierany. Próbowałem zmienić Manifest za pomocą filtrów Intent, ale nic nie działa. Nie są zgłaszane wyjątki, ale IntentService po prostu nie jest wywoływana.IntentService nie jest wywoływany

Oto 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); 

} 

Oto twórca i onHandleIntent klasy IntentService, wiem, że nie jest on nazywany, ponieważ logcat nigdy nie pokazuje „Źródło intencję”. Konstruktor nie jest wywoływana albo (Nie było wywołanie dziennika tam, ale usunięto go.

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 
    } 
} 

A oto 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> 

Odpowiedz

7

Najedź oświadczenie usług poza zakresem TwitterTwoActivity w pliku XML, jak Ja wam uczyniłem tutaj:

<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

Musisz określić ścieżkę do serwisu w swoim manifeście, po prostu umieszczenie nazwy nie jest wystarczające.

Zmień

<service android:name="SyncService"/> 

do

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

jeśli SyncService znajduje się w katalogu głównym pakietu, w razie potrzeby dodaj odpowiedni folder przed numerem .SyncService.

+0

Dzięki, że jest właściwa praktyka, ale jest w odpowiednim folderze, myślę – Jbad26

Powiązane problemy