Question

I try to play with Service in Android, my application contain an Activity with a button so that it can do action when I press it. The core component is the ExtractingURLService , which extends IntentService framework class. I have overrided the onHandleIntent() method, so that it can handle intent passed to it properly. Code:

public  class ExtractingURLService extends IntentService {

    private static final String TAG = "ExtractingURLService"; 

    public ExtractingURLService(String name) {
        super("ExtractingURLService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // do something here !
        }
    }



}

The Android Manifest file is :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="example.service.urlextraction"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ServiceExample02Activity"
                  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=".ExtractingURLService" android:enabled="true"></service>

    </application>
</manifest>

In my only Activity class, I call the service by using:

Intent startServiceIntent =  new Intent(this, ExtractingURLService.class);
startService(startServiceIntent);

That's all my work, but when deploy, the runtime error I received was:

04-09 16:24:05.751: ERROR/AndroidRuntime(1078): Caused by: java.lang.InstantiationException: example.service.urlextraction.ExtractingURLService

What I should do with that ??

Was it helpful?

Solution

Add full path of Service

<service android:name="com.foo.services.ExtractingURLService" android:enabled="true"></service>

Intent Service should look like this:

package com.foo.services;

import android.app.IntentService;
import android.content.Intent;

public class ExtractingURLService extends IntentService {

    private static final String TAG = "ExtractingURLService";

    public ExtractingURLService() {
        super("ExtractingURLService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // do something here !
    }
}

OTHER TIPS

You can add try and catch block to avoid crash.Its work for me

try {
      Intent intent = new Intent(DashboardActivity.this, MyService.class);
      startService(intent);

     }catch (IllegalStateException e){

    }catch (Exception ee){

   }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top