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();
}
}
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();
}
}
wow, Just what I was looking for, thanks!!
ReplyDeleteFelipe Arturo: Hmmm , Welcome, Mate
ReplyDelete