1. Check out our companion app, Forums for Android! Download from Google Play

Toast message not showing in service

Discussion in 'Application Development' started by cchang, Feb 19, 2009.

  1. cchang

    cchang New Member
    Thread Starter
    5

    Feb 11, 2009
    2
    0
    5
    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
    5

    May 2, 2009
    3
    0
    5
    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
    5

    Jul 6, 2009
    1
    0
    5
    try this

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

    ahmdalitaha
    MyBlog
     
  4. Zeos

    Zeos New Member
    5

    Mar 9, 2010
    1
    0
    5
    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
    5

    Mar 25, 2010
    1
    0
    5
    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...