Imagine I have the following code that runs as a background processor for an android application:
public class Background extends Service
{
public void popup (String message, int duration)
{
Toast.makeText(this, message, duration).show();
}
class BackgroundChecker extends TimerTask
{
public void run()
{
popup("Message!", Toast.LENGTH_LONG); // here
}
}
}
When it reaches the popup message i.e. // here, Android tells me to force quit. I know that the problem is with context i.e. this, but I don’t know why because I have extended Service in that class which Android API told me to. Can you help me figure out why this is happening and how to fix it? By the way, instead of this I used getApplicationContext() as well but it still crashes =(.
Thank you.
I havent tried this, but how about having your service tell an activity to do a toast, since a Service is a background activity it seems like its context wouldnt work to do a toast.
Something else you could try is to extend the Application class, and have a toast generator there, and then from your service call.
Your ApplicationExtender:
You need to add your extending class to your manifest for it to work.
I cant garantee this will work, I havent tried it.