java.lang.ClassCastException而在`Service`类运行新的`runnableThread` [英] java.lang.ClassCastException while running new `runnableThread` in `Service` Class

查看:430
本文介绍了java.lang.ClassCastException而在`Service`类运行新的`runnableThread`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,包含了一个广播接收器监听到所有新收到的短信
。如果有任何新的SMS收到此广播接收器启动后台服务它运行的GPS和返回的坐标,这个坐标检索需要新的线程来运行,在服务类没有 getApplicationContext()所以我用'getContentBase()'启动新线程,这是code

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

((活动)getBaseContext())。runOnUiThread(新的Runnable(){

((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);
                    }
                }

和我得到这个例外

 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));

    }

和异常是在此行

                ((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??

推荐答案

首先,服务扩展 ContextWrapper 因此,是一个上下文。如果你需要一个上下文的引用,你可以简单地引用您的服务。你不能投了服务的基本上下文给活动

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.

如果你想从一个服务UI线程工作,看看创建一个处理程序 Looper.getMainLooper()

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);
            });
...

在Android文档提供到UI线程传递好的信息。看看这里:

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

http://developer.android.com/training/multiple-线程/沟通,ui.html

这篇关于java.lang.ClassCastException而在`Service`类运行新的`runnableThread`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆