【问题标题】:Intent content is not being parsed to intentservice意图内容未解析为意图服务
【发布时间】:2018-04-13 14:22:49
【问题描述】:

我无法将带有操作的意图传递给意图服务。可以启动服务,并且意图不为空,但操作为空。使用 putExtra 时也会出现同样的问题。

当我到达第一个断点时,我可以看到操作已被填充,但是当我到达 onHandleIntent 方法中的断点时,我的操作是空的。 该操作在 onStart 方法和 onStartCommand 方法中也已经为空。

我已经包含了我的 intentservice 类的代码。我使用 getNewDeck 方法作为辅助方法,这样我就可以从各种活动中调用此方法,而无需创建意图或忘记某些参数。这个帮助方法启动服务。但是我已经从一个活动中启动了服务并且问题仍然存在,所以我认为问题不在于那里。

public class MyService extends IntentService {

public static final String TAG="MyService";
public static final String MY_SERVICE_MESSAGE="myServiceMessage";
public static final String MY_SERVICE_PAYLOAD="myServicePayload";


private static final String SR_GET_NEW_DECK="getNewDeck";
private static final String SR_DRAW_CARDS_FROM_DECK="drawCardsFromDeck";

private static final String PARAM_DECK_ID="deckID";

private static final String PARAM_DRAW_CARDS_FROM_DECK_COUNT="count";



private DeckApiService deckApiService;
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 */
public MyService() {
    super("MyService");
}



public static void getNewDeck(Context context) {
    Intent i=new Intent(context,MyService.class);
    i=i.setAction(SR_GET_NEW_DECK);
    context.startService(i); //breakpoint (action is "getNewDeck" here.)
}




@Override
protected void onHandleIntent(Intent intent) {
    Intent messageIntent=null;

    deckApiService=DeckApiService.retrofit.create(DeckApiService.class);


    if (intent != null) {

        final String action = intent.getAction(); // breakpoint (intent.getAction() is null here)

        try {
            switch (action) {
                case SR_GET_NEW_DECK:
                    //Geeft een nieuw deck terug, het deckid is op te vragen
                    messageIntent = handleGetNewDeck();
                    break;

                case SR_DRAW_CARDS_FROM_DECK:
                    //Geeft een pile terug met getrokken kaarten uit een deck
                    //in de intent zijn deckID en het aantal te trekken kaarten meegegeven
                    messageIntent = handleDrawCardsFromDeck(intent);
                    break;

                default:
                    Log.d(TAG, "onHandleIntent: Wrong parameters");
                    messageIntent = null;
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "onHandleIntent: Wrong intent");
            messageIntent = null;
        }
    }


    if(messageIntent!=null) {
        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getApplicationContext());
        manager.sendBroadcast(messageIntent);
    }

}

【问题讨论】:

    标签: android android-intent action intentservice


    【解决方案1】:

    这部分代码没有造成问题,我在另一个方法中以空意图启动了意图服务。在第一次调用完成后使用上面代码中创建的意图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多