IntentService未被调用

7

我正在尝试在Activity的onCreate方法中发送一个Intent来启动IntentService,但是IntentService的onHandleIntent方法似乎没有接收到。我已经尝试过更改Manifest文件中的Intent-filters,但似乎没有效果。没有抛出异常,但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>
2个回答

9
您需要在清单文件中定义服务的路径,仅仅放置名称不足够。
请注意修改。
<service android:name="SyncService"/>

to

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

如果 SyncService 在您的软件包根目录中,则在必要的情况下添加相应的文件夹,然后在 .SyncService 前加上该文件夹的名称。

谢谢,这是正确的做法,但我认为它已经在正确的文件夹中了。 - Jbad26

9

将服务声明移到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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接