Why is my service not working on Android? (I just want to write something every 5 seconds)

I created a new HelloService class. I added this to the Android manifest. Xml

public class HelloService extends Service { private Timer timer = new Timer(); private long INTERVAL = 5000; public void onCreate() { super.onCreate(); startservice(); } private void startservice() { timer.scheduleAtFixedRate( new TimerTask() { public void run() { Log.d("servy", "This proves that my service works."); } }, 0, INTERVAL); ; } private void stopservice() { if (timer != null){ timer.cancel(); } } @Override public IBinder onBind(Intent arg0) { return null; } } 

My other activity calls it this way:

  Intent helloservice = new Intent(this, HelloService.class); startService(helloservice); 

For some reason, I set a breakpoint in my new HelloService ... but it didn't even hit. He also does not register.

Edit: "Unable to start Intent service {cmp = com.examples.hello / .HelloService}: not found"

What does this mean? ... I created HelloService.java in the same place as everything else ...




solved. I fixed the manifest file. Thanks Nikola Smiljanic

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

at

  <service android:name="HelloService"></service> 
+18
java android timer service
Feb 15 2018-10-15T00
source share
5 answers

You may not be declaring the service explicitly. In any case, you are using the wrong class. You must use AlarmManager to program events. See this link, it was very useful for me.

Use alarmManager and the service to schedule only for a specific period of time.

+7
Dec 13 '11 at 22:47
source share

The service has a life cycle, like any other Android application. For this reason, it may happen that your service is killed by the system (see Service ). The correct way to implement this is with the Alarm Manager , as discussed in stopping the Android service ,

+3
Sep 09 '12 at 13:11
source share

You will try this:

 helloservice.setComponent(new ComponentName (*hello service package name goes here*, *hello service canonical name goes here*)); startService(helloservice); 
+1
Sep 15 '11 at 15:35
source share

Declare your service in the mainfest.xml file of your project.

 <services android:name=".SMSReceiver" android:enabled="true"> <intent-filter> <action android:name=/> </intent-filter> </services> 
0
Dec 14 '11 at 11:24
source share
0
Dec 14 '11 at 19:07
source share



All Articles