【问题标题】:Activity cant find Bundle in IntentActivity 在 Intent 中找不到 Bundle
【发布时间】:2015-11-05 20:23:11
【问题描述】:

我正在使用IntentService(第一次),我打算通过使用 Bundle 来返回此服务的结果。但是,当我这样做时,主要活动没有找到Bundle,返回null。什么可能导致这种情况?字符串键匹配!

下面的代码输出:

I/System.out: It's null

主要活动:

public class MainMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Some stuff here...

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(StorageHandler.TRANSACTION_DONE);
        registerReceiver(broadcastReceiver, intentFilter);
        Intent i = new Intent(this, StorageHandler.class);
        startService(i);
    }



    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extra = getIntent().getBundleExtra("bundle");

            if(extra == null){
                System.out.println("It's null");
            }
            else {
                ArrayList<String> objects = (ArrayList<String>) extra.getSerializable("objects");
                System.out.println(objects.get(0));
            } 
        }
    };

}

IntentService:

public class StorageHandler extends IntentService{

    public static final String TRANSACTION_DONE = "xx.xxxxx.xxxxxx.TRANSACTION_DONE";

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

    public void onCreate(){
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        notifyFinished();
    }

    private void notifyFinished(){
        ArrayList<String> objects = new ArrayList<String>();
        objects.add("Resulting Array here");

        Bundle extra = new Bundle();
        extra.putSerializable("objects", objects);

        Intent i = new Intent();
        i.setAction(xx.xxxxx.xxxxxx.StorageHandler.TRANSACTION_DONE);
        i.putExtra("bundle", extra);

        StorageHandler.this.sendBroadcast(i);
    }

【问题讨论】:

    标签: android android-intent bundle intentservice


    【解决方案1】:

    您正试图从错误的Intent 检索数据。

    变化:

    Bundle extra = getIntent().getBundleExtra("bundle");
    

    收件人:

    Bundle extra = intent.getBundleExtra("bundle");
    

    包含您的数据的Intent 作为BroadcastReceiveronReceive() 方法的参数之一提供。

    【讨论】:

      【解决方案2】:

      您正在使用getIntent() 检索广播意图。这是错误的。您必须使用的意图是onReceive 的前一个参数。改变

      Bundle extra = getIntent().getBundleExtra("bundle");
      

      Bundle extra = intent.getBundleExtra("bundle");
      

      【讨论】:

        【解决方案3】:

        只需在您的 Activity 中使用它:

        onResume回调中你应该注册registerReceiver(broadcastReceiver, intentFilter);

        onPause 回调中,您应该取消注册此接收器。在你的接收器中使用这个:

        Bundle extra = intent.getBundleExtra("bundle");

        在您的服务中使用此代码:

         Intent i = new Intent(TRANSACTION_DONE).putExtra("bundle", extra);
                    this.sendBroadcast(i);
        

        更多信息,请参阅This Answer

        【讨论】:

          【解决方案4】:

          不要在你的 onCreate MainActivity 中忘记这一点:

          protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
          
               //All your code here
           }
          

          我这样说是因为我在您的方法中没有看到该代码行!

          【讨论】:

          • 这包含在方法的“Some stuff here...”区域中。但是感谢您的提醒。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-24
          • 1970-01-01
          • 1970-01-01
          • 2016-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多