I have an android activity which runs a remote service and then quits. The service itself, polls on a device node, and checks for changes, I want to use toast to alert the user, but I didn't mange to get it to work. the Toast is not showing, and after a while, Android shouts that my app is not responding. BTW, I don't want to start the activity again and display the toast from there, I just want it to pop on the current screen shown to the user.
Here is the service code:
public class MainService extends Service {
// Native methods
public native static int GetWiegandCode();
public native static void openWiegand();
public native static void closeWiegand();
static int code = 0;
// Other
private static final String TAG = MainService.class.getSimpleName();
private Handler handler;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void run() {
Handler h;
while (true) {
code = GetWiegandCode();
if (code > 0) {
h = new Handler(this.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(),
"ID " + Integer.toString(code) +
"Just entered", Toast.LENGTH_LONG).show();
}
});
}
}
}
@Override
public void onCreate() {
super.onCreate();
openWiegand();
Log.i(TAG, "Service Starting");
this.run();
}
@Override
public void onDestroy() {
super.onDestroy();
closeWiegand();
Log.i(TAG, "Service destroying");
}
static {
System.loadLibrary("wiegand-toast");
}
}