代码之家  ›  专栏  ›  技术社区  ›  Arne

未将意图内容解析为intentservice

  •  1
  • Arne  · 技术社区  · 8 年前

    我无法将意图和动作传递给intentservice。可以启动服务,并且意图不为null,但操作为null。使用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);
        }
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Arne    8 年前

    这段代码中没有产生问题,我在另一个方法中启动了intentservice,但意图为空。在上述代码中创建的意图是在第一次调用完成后使用的。

    推荐文章