Its a very small issue to display toast notification from the service as many think that toast is linked to activity it is not like toast is linked with the application context that is why we just have to create a handler in the service which just run with the main ui thread here is one way to figure out this issue.
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();
}
}
Hope this code will help
any query drop in comments
enjoy programming!!!
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();
}
}
Hope this code will help
any query drop in comments
enjoy programming!!!