如何将我的广播接收器上下文类传递给我的服务类

时间:2012-07-24 19:00:35

标签: sms broadcast

  

可能重复:
  Static Way to get Context on android?

我有一个广播接收器类和服务类,如果我收到一个字符串“testing”,我将在我的广播接收器中启动我的服务。

这就是我在广播接收器类中开始服务的方式。它正在运作

private void startSMSforwarderService(Context context,String y)
{
    if(y.startsWith("testing"))
    {
        Intent SmsForwarderService = new Intent(context,SmsForwarderService.class);
        SmsForwarderService.putExtra("var_msgbody",msgbody); //pass variable value via intent from broadcast receiver to android service
        SmsForwarderService.putExtra("var_from", from);

        context.startService(SmsForwarderService);
    }
}

我的服务中有一个需要上下文的方法,我想将我的上下文从我的广播接收器扩展到我的服务。我怎么能这样做?一些片段非常感谢..谢谢

我的接收者 https://gist.github.com/3171877

我的服务类

https://gist.github.com/3171792

正如你所看到的,我的服务中有一个方法,它需要一个上下文才能使用它..

void forwardSMS(String msgbody,Context smsSenderService, Intent SENT_ACTION)

基本上我想将广播接收器上下文传递给我的服务类....

1 个答案:

答案 0 :(得分:0)

问题解决了..我找到了这个Static way to get 'Context' on Android?

但他们是另一种方式吗?