java.lang.ClassCastException while running new `runnableThread` in `Service` Class
Asked Answered
C

1

2

I have an application , contains a broadcastReceiver which listens to all new received SMS . If any new SMS received this BroadcastReceiver starts a background Service which runs GPS and return the coordinates , this coordinate retrieving needs a new thread to be run , in the Service class there is no getApplicationContext() so I used 'getContentBase()' for starting the new thread this is the code

((Activity)getBaseContext()).runOnUiThread(new Runnable() {

                ((Activity)getBaseContext()).runOnUiThread(new Runnable() {


                        @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                        LocationListener locationListener = new MyLocationListener();
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER, 5000, 10,
                                locationListener);
                    }
                }

and I am getting this exception

 E/AndroidRuntime(24700): java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity

this is how the receiver defined in the manifest

    <receiver
        android:name=".BroadCastReceiver"
        android:exported="true" >
        <intent-filter android:priority="1000" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

and here is how the Service started in the BroadcastReceiver

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    context.startService(new Intent(con,
            MyService.class));

    }

and the exception is at this line

                ((Activity)getBaseContext()).runOnUiThread(new Runnable() 

I've searched a lot through the stack and the google as well , non of the answers solved my issue and tried to send the context from the BroadCastReceiver to the Service class , again there was the same Exception , any help??

Coin answered 27/4, 2014 at 13:48 Comment(0)
L
3

First of all, Service extends ContextWrapper and is therefore a Context. If you need a reference to a Context you can simply reference your Service. You cannot cast a Service's base Context to an Activity.

If you want to work on the UI Thread from a Service, have a look at creating a Handler with Looper.getMainLooper().

...
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
            @Override
            public void run() {
                LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                LocationListener locationListener = new MyLocationListener();

                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
            });
...

The Android documentation offers good information on communicating to the UI thread. Have a look here:

http://developer.android.com/training/multiple-threads/communicate-ui.html

Lanate answered 27/4, 2014 at 14:10 Comment(1)
thank youuuuuuuuuuuuuuuuu , you saved my life , you are an angle that god has sent you to me . I dont know how to express my happiness because you know , i will earn like 20000$ after fixing this error and finishing the appCoin

© 2022 - 2024 — McMap. All rights reserved.