从广播接收器调用时故意服务不上手 [英] Intent Service do not get started when called from a broadcast receiver

查看:105
本文介绍了从广播接收器调用时故意服务不上手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序中,在接收到的消息,我需要启动服务。但我的意图服务没有运行。下面是我在做什么:

I am working on an app in which on receiving a message i need to start a service. But my Intent service is not running. Here is what I am doing:

广播接收器:

public void onReceive(Context context, Intent intent) 
{
    this.con=context;
    Toast.makeText(context,"Broadcast received", Toast.LENGTH_LONG).show();

    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[])bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    for(int i=0;i<messages.length;i++)
    {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }

    String smsFrom = sms[0].getDisplayOriginatingAddress().toString();

    Intent in= new Intent(context, ResponseService.class);
    in.putExtra("sender",smsFrom);
    context.startService(in);
}

IntentService:

public class ResponseService extends IntentService 
{
    public ResponseService(String name) 
        {
        super(name);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) 
        {
        // TODO Auto-generated method stub
        Log.d("Notify", "In Response Service");
        getCurrentLocation();
        }
}

我不明白在日志文件中任何事情。任何人可以帮我理解这个问题?先谢谢了。

I don't get anything in the Log file. can anybody please help me understanding the problem? Thanks in advance.

更新
我已经宣布的manifest.xml两个广播接收机和服务如下:

Update I already have declared both broadcast receiver and service in manifest.xml as follows:

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

    <service android:name=".ResponseService" />

此外我试图再次运行应用程序,并经过一段时间的应用程序强制关闭给人一种java.lang.instantiationexception
有人可以帮我解决这个问题?

Moreover I again tried to run the app and after some time the app force closed giving a java.lang.instantiationexception Can somebody please help me with this problem?

推荐答案

外,java.lang.InstantiationException确实解决了我的问题。我缺少一个无参数的公共构造方法。我只是改变了

The exception, java.lang.InstantiationException really solved my problem. I was missing a zero argument public constructor. I just changed

public ResponseService(String name) 
        {
        super(name);
        // TODO Auto-generated constructor stub
    }

进入:

public ResponseService() 
        {
        super("ResponseService");
        // TODO Auto-generated constructor stub
    }

而现在它的工作真的很好。希望它可以帮助其他人。

And now it's working really well. Hope it might help others as well.

这篇关于从广播接收器调用时故意服务不上手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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