I created a new class called HelloService. 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 like this:
Intent helloservice = new Intent(this, HelloService.class);
startService(helloservice);
For some reason, I put a breakpoint in my new HelloService...but it's not even hitting. It's not logging either.
Edit: "Unable to start service Intent { cmp = com.examples.hello/.HelloService }: not found"
What does that mean? ... I created HelloService.java in the same place as everything else...
Solved. I fixed my manifest file. Thanks Nikola Smiljanic
<service android:name=".HelloService"/>
to:
<service android:name="HelloService"></service>
Source: Tips4all
Maybe you're not declaring the service in your manifiest. Anyway you're using the wrong class. You have to use AlarmManager for programing events. See that link, it was very useful to me.
ReplyDeleteUse alarmManager and service to perform schedule notification only during specific time period
Will you try this:
ReplyDeletehelloservice.setComponent(new ComponentName
(*hello service package name goes here*,
*hello service canonical name goes here*));
startService(helloservice);
Declare the your service in mainfest.xml file of your project.
ReplyDelete<services android:name=".SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name=/>
</intent-filter>
</services>
You need to implement onStartCommand()
ReplyDeletehttp://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent,int,int)