Thursday, March 17, 2011

Dispalying Toast Message in a Service in Android?

Hi All, Let us how to create Toast Message in Service

 Step 1:  Create a Handler and thread as i shown below

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

public class MyService extends Service{

String msg="I m Toast";

Thread t = new Thread(){
public void run(){

Message myMessage=new Message();
Bundle resBundle = new Bundle();
resBundle.putString("status", "SUCCESS");
myMessage.obj=resBundle;
handler.sendMessage(myMessage);
}
};



private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
}
};


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub

return null;
}

@Override
public void onCreate() {
 t.start();
}


}


2 comments:

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...