Service stopping and not restarting

Put your problem here if it does not fit any of the other categories.

Service stopping and not restarting

Postby cchang » Tue Mar 03, 2009 12:22 pm

Hi,

I have a BroadcastReceiver that executes when the system boots (<action android:name="android.intent.action.BOOT_COMPLETED"/>), then it starts a service with the startService() method. My service starts and then I have a timer to repeatingly execute a query to a content provider i have. It works good first time, but then it stops i guess by the activity manager. I see this on the DDMS: "Scheduling restart of crashed service com.criticalsoftware.meo/.AlertService in 5000ms", but the service never restarts.

I'm trying a different approach now, using alarm manager, but been having the same result. Can anyone help??

Thanks in advance.
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Top

Re: Service stopping and not restarting

Postby wilken » Tue Mar 03, 2009 1:46 pm

cchang wrote:Hi,

I have a BroadcastReceiver that executes when the system boots (<action android:name="android.intent.action.BOOT_COMPLETED"/>), then it starts a service with the startService() method. My service starts and then I have a timer to repeatingly execute a query to a content provider i have. It works good first time, but then it stops i guess by the activity manager. I see this on the DDMS: "Scheduling restart of crashed service com.criticalsoftware.meo/.AlertService in 5000ms", but the service never restarts.

I'm trying a different approach now, using alarm manager, but been having the same result. Can anyone help??

Thanks in advance.


did you set a wakelock? otherwise the service will shut down when the phone sleeps.

that said, i have a setup where my app starts a service and starts an alarmmanager that calls a broadcast receiver that calls the service, to update a contentprovider.. this seems to work.
i dont know if there is a difference in that you want the service to be started by the boot event?
wilken
Developer
Developer
 
Posts: 35
Joined: Wed Oct 08, 2008 8:22 am

Re: Service stopping and not restarting

Postby cchang » Tue Mar 03, 2009 3:22 pm

wilken wrote:
cchang wrote:Hi,

I have a BroadcastReceiver that executes when the system boots (<action android:name="android.intent.action.BOOT_COMPLETED"/>), then it starts a service with the startService() method. My service starts and then I have a timer to repeatingly execute a query to a content provider i have. It works good first time, but then it stops i guess by the activity manager. I see this on the DDMS: "Scheduling restart of crashed service com.criticalsoftware.meo/.AlertService in 5000ms", but the service never restarts.

I'm trying a different approach now, using alarm manager, but been having the same result. Can anyone help??

Thanks in advance.


did you set a wakelock? otherwise the service will shut down when the phone sleeps.

that said, i have a setup where my app starts a service and starts an alarmmanager that calls a broadcast receiver that calls the service, to update a contentprovider.. this seems to work.
i dont know if there is a difference in that you want the service to be started by the boot event?



I didn't set a wavelock, i'll look into it now. As for for the alarm manager, can you paste the most important parts of your code you just mentioned? I think it would be very helpful. I'll try and find a solution for that now.

Thanks :)
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Re: Service stopping and not restarting

Postby wilken » Tue Mar 03, 2009 3:26 pm

cchang wrote:
wilken wrote:
cchang wrote:Hi,

I have a BroadcastReceiver that executes when the system boots (<action android:name="android.intent.action.BOOT_COMPLETED"/>), then it starts a service with the startService() method. My service starts and then I have a timer to repeatingly execute a query to a content provider i have. It works good first time, but then it stops i guess by the activity manager. I see this on the DDMS: "Scheduling restart of crashed service com.criticalsoftware.meo/.AlertService in 5000ms", but the service never restarts.

I'm trying a different approach now, using alarm manager, but been having the same result. Can anyone help??

Thanks in advance.


did you set a wakelock? otherwise the service will shut down when the phone sleeps.

that said, i have a setup where my app starts a service and starts an alarmmanager that calls a broadcast receiver that calls the service, to update a contentprovider.. this seems to work.
i dont know if there is a difference in that you want the service to be started by the boot event?



I didn't set a wavelock, i'll look into it now. As for for the alarm manager, can you paste the most important parts of your code you just mentioned? I think it would be very helpful. I'll try and find a solution for that now.

Thanks :)


Hi,
i'm not near my code right now, but i more or less used this page:

http://www.androidcompetencycenter.com/ ... rmmanager/

to set it up
wilken
Developer
Developer
 
Posts: 35
Joined: Wed Oct 08, 2008 8:22 am

Re: Service stopping and not restarting

Postby cchang » Tue Mar 03, 2009 3:27 pm

wilken wrote:
Hi,
i'm not near my code right now, but i more or less used this page:

http://www.androidcompetencycenter.com/ ... rmmanager/

to set it up


Ok. Thanks very much
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Postby cchang » Tue Mar 03, 2009 5:15 pm

Sorry for troubling you. I implement my service to start now only when my application is starting and it's working fine (I used the timer approach, have to try the alarm one later), but i'm running to another problem, it seems like my message:

Toast.makeText(AlertService.this, "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG);

is not displaying and I can't figure out why. Have you run into similar issue before?

Here's my service code:

public class AlertService extends Service {
private Timer timer;
private TimerTask executeAgain = new TimerTask() {
@Override
public void run() {
ContentResolver cr = getContentResolver();
Log.i("AlertService", "AlertService executing...");
Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG);

//Do something
}
};

@Override
public void onStart(Intent intent, int startId) {
if (timer == null) {
timer = new Timer("Alert timer");
}

timer.scheduleAtFixedRate(executeAgain, 0, 10000);
}

@Override
public void onDestroy() {
Log.i("AlertService", "AlertService is being destroyed!!!");
}

@Override
public void onCreate() {
timer = new Timer("Alert timer");
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Top

Postby MrSnowflake » Tue Mar 03, 2009 6:20 pm

Change Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG) to Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG).show();
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby cchang » Tue Mar 03, 2009 6:28 pm

MrSnowflake wrote:Change Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG) to Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG).show();


I had that before, it doens't work also. It's weird but no code after that instruction is executed, it's as it's throwing some exception or something, but nothing is happening on the DDMS (I even have it in verbose). I have to check this better.

UPDATE

I put a try catch around the instruction and I got this:

03-03 17:36:31.300: WARN/System.err(944): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-03 17:36:31.311: WARN/System.err(944): at android.os.Handler.<init>(Handler.java:111)
03-03 17:36:31.323: WARN/System.err(944): at android.widget.Toast.<init>(Toast.java:397)
03-03 17:36:31.390: WARN/System.err(944): at android.widget.Toast.makeText(Toast.java:230)
03-03 17:36:31.426: WARN/System.err(944): at com.criticalsoftware.meo.AlertService$1.run(AlertService.java:27)
03-03 17:36:31.430: WARN/System.err(944): at java.util.Timer$TimerImpl.run(Timer.java:277)
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Postby wilken » Tue Mar 03, 2009 11:51 pm

cchang wrote:
MrSnowflake wrote:Change Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG) to Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG).show();


I had that before, it doens't work also. It's weird but no code after that instruction is executed, it's as it's throwing some exception or something, but nothing is happening on the DDMS (I even have it in verbose). I have to check this better.

UPDATE

I put a try catch around the instruction and I got this:

03-03 17:36:31.300: WARN/System.err(944): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-03 17:36:31.311: WARN/System.err(944): at android.os.Handler.<init>(Handler.java:111)
03-03 17:36:31.323: WARN/System.err(944): at android.widget.Toast.<init>(Toast.java:397)
03-03 17:36:31.390: WARN/System.err(944): at android.widget.Toast.makeText(Toast.java:230)
03-03 17:36:31.426: WARN/System.err(944): at com.criticalsoftware.meo.AlertService$1.run(AlertService.java:27)
03-03 17:36:31.430: WARN/System.err(944): at java.util.Timer$TimerImpl.run(Timer.java:277)


hi,
the problem is that you try to show a toast in a non ui thread. the preferred method for letting a service show information is through the notification system (so you show an icon in the status bar)

i dont know how to show a toast when you have no ui running, sorry.
wilken
Developer
Developer
 
Posts: 35
Joined: Wed Oct 08, 2008 8:22 am

Postby cchang » Wed Mar 04, 2009 1:11 am

wilken wrote:
cchang wrote:
MrSnowflake wrote:Change Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG) to Toast.makeText(getApplicationContext(), "LLLLLLLLLLLLLLLLLLLLLLLLL", Toast.LENGTH_LONG).show();


I had that before, it doens't work also. It's weird but no code after that instruction is executed, it's as it's throwing some exception or something, but nothing is happening on the DDMS (I even have it in verbose). I have to check this better.

UPDATE

I put a try catch around the instruction and I got this:

03-03 17:36:31.300: WARN/System.err(944): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-03 17:36:31.311: WARN/System.err(944): at android.os.Handler.<init>(Handler.java:111)
03-03 17:36:31.323: WARN/System.err(944): at android.widget.Toast.<init>(Toast.java:397)
03-03 17:36:31.390: WARN/System.err(944): at android.widget.Toast.makeText(Toast.java:230)
03-03 17:36:31.426: WARN/System.err(944): at com.criticalsoftware.meo.AlertService$1.run(AlertService.java:27)
03-03 17:36:31.430: WARN/System.err(944): at java.util.Timer$TimerImpl.run(Timer.java:277)


hi,
the problem is that you try to show a toast in a non ui thread. the preferred method for letting a service show information is through the notification system (so you show an icon in the status bar)

i dont know how to show a toast when you have no ui running, sorry.


Thanks for your input wilken. I read the same while browsing for a solution to my problem. I'll try and work it out somehow. Thanks again man :).
cchang
Freshman
Freshman
 
Posts: 8
Joined: Mon Feb 23, 2009 4:40 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests