2012-07-13 76 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