Toast message not showing in service


Last Updated:

  1. cchang

    cchang New Member This Topic's Starter

    Joined:
    Feb 11, 2009
    Messages:
    2
    Likes Received:
    0
    Hi! I'm having trouble coding a service example. My service is a very simple one that only displays a message at a given interval. Here's my code:

    AndroidManifest.xml

    <service android:name=".BuggingService" android:enabled="true" />

    BuggingService.java

    public class BuggingService extends Service {
    private Timer timer;
    private int counter;
    private TimerTask executeAgain = new TimerTask() {
    @Override
    public void run() {
    Toast.makeText(getApplicationContext(), "I poop on you", Toast.LENGTH_LONG).show();
    }
    };

    @Override
    public void onCreate() {
    timer = new Timer("buggingService");
    timer.scheduleAtFixedRate(executeAgain, 0, 5000);
    }

    @Override
    public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
    }
    }

    Hello.class

    startService(new Intent(this, BuggingService.class));

    The service is running, but the message is never displayed and I don't understand why. If I debug my service the timer is executing every 5 seconds. Can anyone offer an explanation for this?

    Thanks in advance.
     

    Advertisement
  2. fixedd

    fixedd New Member

    Joined:
    May 2, 2009
    Messages:
    3
    Likes Received:
    0
    I'd like an answer to this too. Mine is slightly different that this (I'm calling from another thread), but close enough that I think it's the same problem.
     
  3. ahmdalitaha

    ahmdalitaha New Member

    Joined:
    Jul 6, 2009
    Messages:
    1
    Likes Received:
    0
    try this

    Looper.prepare();
    Toast.makeText(getApplicationContext(), "I poop on you", Toast.LENGTH_LONG).show(); Looper.loop();

    ahmdalitaha
    MyBlog
     
  4. Zeos

    Zeos New Member

    Joined:
    Mar 9, 2010
    Messages:
    1
    Likes Received:
    0
    I'm sorry, but I have the same problem, and this tip does'n hepl me.
    I write this code:
    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void onCreate()
    {
    super.onCreate();


    timer.scheduleAtFixedRate(
    new TimerTask() {
    public void run() {
    getAdsFromServer();
    }
    },
    0,
    UPDATE_INTERVAL);
    }

    private void getAdsFromServer()
    {
    Log.v("stop11", "stop11");
    Looper.prepare();
    Toast.makeText(etApplicationContext(), "I poop on you", Toast.LENGTH_LONG).show();
    Looper.loop();
    }

    But even Log.v in this case write in log only one time.

    If I doesn't use Looper, Log.v working, but toasts doesn't show.
     
  5. Yordan

    Yordan New Member

    Joined:
    Mar 25, 2010
    Messages:
    1
    Likes Received:
    0
    Try this:
    //a member
    private Handler handler;

    //
    public void onCreate() {
    ...
    handler = new Handler(Looper.getMainLooper());
    ...
    timer.scheduleAtFixedRate(
    new TimerTask() {
    public void run() {
    getAdsFromServer();
    }
    },
    0,
    UPDATE_INTERVAL);
    }

    private void getAdsFromServer()
    {
    Log.v("stop11", "stop11");

    handler.post(new Runnable() {

    public void run() {
    Toast.makeText(etApplicationContext(), "I poop on you", Toast.LENGTH_LONG).show();
    }
    });


    }
     

Share This Page

Loading...