0

I´m a student learning how to crate an android app. currently i´m learning about ServiceIntents. Following my transcript: to test the upload funcionality of a button, i have to use the UploadIntentService-class. But if i write the code exactly how it´s shown in my transcript Android Studio already highlight UploadIntentServicein red.

Here´s the code from the transcript (MainActivity.java):

public void onClickUpload (View button){
    Intent uploadIntent = new Intent(this, UploadIntentService.class);
    startService(uploadIntent);
}

protected void onHandleIntent(Intent intent){
    Log.v(UploadIntentService.class.getSimpleName(), "Service sleeping...");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Log.v(UploadIntentService.class.getSimpleName(), "Service wake up...");
}

Imports:

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.IntentService;

If i try to run the app the build process can´t be done:

cannot find symbol class UploadIntentService
cannot find symbol class UploadIntentService
cannot find symbol class UploadIntentService
uses or overrides a deprecated API.

I´m working with java, not Kotlin.

3474
  • 1
  • 2

1 Answers1

0

This line starts your service UploadIntentService

startService(uploadIntent);

But you need to create this service first, means you need to create a new class, which extends IntentService and which is doing actual upload (or whatever you want it to do). Example:

public class UploadIntentService extends IntentService {

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

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        // put your code here to upload
    }
}

Service should also be registered in AndroidManifest.xml in application section. Example:

<application>
.....
    <service
          android:name="com.example.myapplication. UploadIntentService"
          android:enabled="true"
    />
</application>
Myroslav
  • 896
  • 12
  • 21
  • hi, thanks for the quick answer. now the problems are not showing anymore, but if i click on the button that is connected to the "onClickUpload" method, in logcat should be shown first the message "Service sleeping..." and after 10 sec "Service wake up.." but this is not happening. can you help with this problem, too? – 3474 Apr 20 '22 at 10:56
  • hard to say without seeing your code – Myroslav Apr 21 '22 at 11:11
  • it´s just the code that overrides the `onHandleIntent`-method above. For example: `Log.v(UploadIntentService.class.getSimpleName(), "Service sleeping...");` should show `Servce sleeping ...` in Logcat – 3474 Apr 21 '22 at 13:30
  • There may be different reasons that come to my mind. Firstly, are you positive that you set `onClickListener` for a button? Assuming that button has id `button`, it's something like ```Button button = findViewById(R.id.button); button.setOnClickListener(v -> { onButtonClicked(); });``` – Myroslav Apr 21 '22 at 17:46
  • I've updated my answer. First, constructor with no parameters should be declared in custom `IntentService`, which calls `super` with some service name as parameter. Second, service should be registered in Manifest file. I've tested this code and it works fine. – Myroslav Apr 21 '22 at 18:18
  • Hi again @Myroslav, so I have to be doing something wrong.. If I try to set the listener the code is always highlighted in some parts. If I try to declare the constructor with no parameters, the service name is highlighted.. if I try to register the service in Manifest the service name is highlighted. Please explain more explicit if possible and sry for my bad coding skills… – 3474 Apr 22 '22 at 15:05
  • now after watching a youtube video i created a intentservice through "new">"service">"intentservice" so that every needed code for a intentservice is generated automatically (also the registration of trhe service in androidmanifest" and wrote all my code in the "onhandleintent" method but still nothing happening.. logcat cant be the problem, because i also tried to just put the "Log.v(.....)" directly in the MainActivity and that worked, the message has been shown there... so i think that the IntentService is somehow not even being started.. – 3474 Apr 22 '22 at 17:19
  • Hi @Myroslav, I´ve got the answer now: the waiting time for the Thread `Thred.sleep(...)` was too long with `100000`. Directly after changing that to `10000` it worked... – 3474 Apr 22 '22 at 17:48