【问题标题】:IntentService doesn't start from BroadcastReceiverIntentService 不是从 BroadcastReceiver 开始的
【发布时间】:2011-11-23 16:09:03
【问题描述】:

我正在尝试从 BroadcastReceiver 启动 IntentService,例如(在 onReceive(Context context, Intent intent) 函数中):

            Intent i = new Intent(context, RegService.class);
            i.putExtra("user_id", userId);
            i.putExtra("device_id", "bla");
            context.startService(i);

和 RegService 一样

public class RegService extends IntentService {

    public RegService(String name) {
        super(name);
    }

    public RegService(){
        super("RegService");    
    }



    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        String c2dm_registration_id = intent
                .getStringExtra("c2dm_registration_id");
        String token = intent.getStringExtra("token");
        String userId = intent.getStringExtra("user_id");
        String device_type = "android";
        try {

            JSONObject object = RestClient.sendC2DMRegistrationId(
                    c2dm_registration_id, device_type,
                    token, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

但我无法在 RegService 中输入 onHandle(我通过了第一个代码 context.startService(i); 但我无法输入 onHandle,没有消息)。有没有人有同样的经验,什么是解决方案? 我有清单

<service
        android:enabled="true"
        android:name=".c2dm.RegService" />

【问题讨论】:

  • 我不太明白你的问题。 “like (in onReceive(Context context, Intent intent) function):” 你指的这个“onReceive”方法在哪里?还有最后一段,“我不能在RegService中输入onHandle”是什么意思?
  • 同时发布您的 manifest.xml 可能会有所帮助,因为我相信您的服务也应该在其中定义。
  • @owengerig 在 BroadcastReceiver 类里面 onReceive 方法我正在使用 context.startService 执行代码。我不能进入意味着程序永远不会到达那个点或功能。但是从上面的代码中你可以看到你可以期望输入那个函数。
  • 不知道具体答案,但我相信名称需要与意图调用的内容相匹配。像这样stackoverflow.com/questions/3439356/…
  • 我认为 RegService 在包中:application_package.c2dm ?

标签: android


【解决方案1】:

当我的 IntentService 在清单中仅使用其类名声明时,我无法从广播接收器中调用它。然后我给出了全名:

<service  android:name="com.abc.def.services.DummyService" />

这成功了,我能够从广播接收器中调用这个 IntentService。

【讨论】:

    【解决方案2】:

    您是否在清单中声明了服务?

    如果是这样并且它不起作用,请尝试对意图设置一个操作,即使您不会使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多