Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runOnUiThread Undefined for Class

I'm trying to throw and alert dialog on the UI thread from my background thread but I'm having problems with runOnUiThread being undefined. I have tried FindLocation.this.runOnUiThread and runOnUiThread but both seem to throw the same error The method runOnUiThread(new Runnable(){}) is undefined for the type new LocationListener(){} (or ...the type FindLocation). Any ideas why? Here is a snippet of my FindLocation.java class. This is called by my main activity.

public class FindLocation extends Thread {

public boolean inJurisdiction;
public boolean AlertNotice = false;
private LocationManager locManager;
private LocationListener locListener;

Context ctx;
public String userId;

public FindLocation(Context ctx) {
     this.ctx = ctx;
}

 public void start(String userId) {
        this.userId = userId;
        super.start();

      }

@Override
public void run() {
     Looper.prepare();
    final String usr = userId;  

    //get a reference to the LocationManager
    locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

    //checked to receive updates from the position
    locListener = new LocationListener() {
        public void onLocationChanged(Location loc) {

            String lat = String.valueOf(loc.getLatitude()); 
            String lon = String.valueOf(loc.getLongitude());

            Double latitude = loc.getLatitude();
            Double longitude = loc.getLongitude();

            if (latitude >= 39.15296 && longitude >= -86.547546 && latitude <= 39.184901 && longitude <= -86.504288 || inJurisdiction != false) {
                Log.i("Test", "Yes");  

                inJurisdiction = true;

                FindLocation.this.runOnUiThread(new Runnable() { ///****error here****
                    public void run() {
                        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
                        alert.setTitle("Sent");
                        alert.setMessage("You will be contacted shortly.");
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int which) {
                           }
                        });
                    }
                });
like image 424
mkyong Avatar asked Apr 03 '12 11:04

mkyong


3 Answers

Since runOnUIThread() is method of Activity, you can pass reference to calling activity in constructor.

...
Context ctx;
Activity act;
public String userId;
...

public FindLocation(Context ctx, Activity act) {
    this.ctx = ctx;
    this.act = act;
}

and use runOnUIThread() like

act.runOnUiThread(new Runnable() {...});

However I believe it's unsafe and you need to take precautions to make sure your Activity is still there when you are calling runOnUiThread

like image 120
Vladimir Avatar answered Oct 01 '22 04:10

Vladimir


Another better approach..

No need to create constructor for getting Activity.

Just typecast the context to Activity class.

((Activity)context).runOnUiThread(new Runnable()
    {
        public void run()
        { 
             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
        }
    });
like image 21
Zar E Ahmer Avatar answered Oct 01 '22 04:10

Zar E Ahmer


runOnUIThread() is a method belonging to Activity.. SO you cannot call it from a Thread.

So instead of Context take Activity instance in its Constructor and call it using that.. something like

activity.runOnUIThread();
like image 23
ngesh Avatar answered Oct 01 '22 04:10

ngesh