0

I'm still new to Android and working my way through a lesson which is trying to teach me how to use an Intent Service to do work on a non-UI thread. However, when I call the Intent Service, nothing seems to happen.

Here is the Intent Service call from MainActivity

startIntentService = (Button) findViewById(R.id.button4);
startIntentService.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick (View v){
        Intent delayIntent = new Intent(getApplicationContext(), 
        DelayIntentService.class);
        startService(delayIntent);

Here is the DelayIntentService code

public class DelayIntentService extends IntentService {
    public static final String ACTION_DELAY =
            "hinz.don.hour5.action.DELAY";
    public static final String EXTRA_MESSAGE =
            "hinz.don.hour5.extra.MESSAGE";
    public DelayIntentService() {
        super("DelayIntentService");
    }

    @Override
    protected void onHandleIntent (Intent intent){
        SystemClock.sleep(5000);
        Intent broadcastIntent = new Intent ();
        broadcastIntent.setAction(ACTION_DELAY);
        broadcastIntent.putExtra(EXTRA_MESSAGE, "UPDATED USING INTENT SERVICE");
        sendBroadcast(broadcastIntent);

and here is the service declaration from the Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hinz.don.hour5">

    <service android:name="hinz.don.Hour5.DelayIntentService"></service>

I've placed a debug break on the Intent Service code but it never executes it

chornge
  • 2,021
  • 3
  • 28
  • 37
Pickles57
  • 13
  • 2
  • set a breakpoint on startService in your MainActivity, does it break there? it should break there as soon as you tap on the button (R.id.button4) – chornge Sep 27 '17 at 05:20
  • 1
    also, there's a typo, your package name in your manifest doesn't match the full service name - there's a capitalized H (Hour5) in the full service name. – chornge Sep 27 '17 at 05:43

3 Answers3

0

In your code there is a miss match with package="hinz.don.hour5" and "hinz.don.Hour5.DelayIntentService". Package used as hours5 and service name used as Hours5. Better to use this <service android:name=".DelayIntentService" /> this will eliminate the ambiguity

0

The Service declaration needed to be placed at the bottom of the Manifest file after the Main Activity declaration. Once I moved it everything worked fine.

Pickles57
  • 13
  • 2
0

your Xml Should be like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hinz.don.hour5">
<applcation .......>

    <service android:name="hinz.don.hour5.DelayIntentService"></service>
</application>

if this not work refer

https://stackoverflow.com/a/15772151/8416317

Vinesh Chauhan
  • 1,288
  • 11
  • 27