Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.

Here is the 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);

}

Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.

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

And here is the 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>
like image 764
Jbad26 Avatar asked Jul 13 '12 18:07

Jbad26


People also ask

Why is IntentService deprecated?

This constant was deprecated in API level 23. MODE_MULTI_PROCESS does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. Applications should not attempt to use it.

How do I start IntentService?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.

Can an IntentService execute multiple tasks sequentially?

The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.


2 Answers

Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:

<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>
like image 151
Richard Avatar answered Oct 06 '22 09:10

Richard


You need to define the path to the service in your manifest, simply putting the name is not sufficient.

Change

<service android:name="SyncService"/>

to

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

if SyncService is in the root of your package, add respective folder before .SyncService if needed.

like image 31
Jug6ernaut Avatar answered Oct 06 '22 09:10

Jug6ernaut