繁体   English   中英

IntentService 在构建他后不保存我的对象

[英]IntentService doesn't save my object after building him

我遇到了一个奇怪的情况,我构建的对象在 onHandleIntent 方法中返回 null,我不知道为什么或如何解决它。

我的活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // the bluetooth is mentioned in case you know a way to use it with out transfer it from the activity
        BluetoothManager _bluetooth_manager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        myService = new MyService(new MyClass(_bluetooth_manager.getAdapter()));

        Intent intent = new Intent(this, MyService.class);
        startService(intent);
}

我的服务

private MyClass X;

 public MyService(MyClass x) {
    super("man");    //not so much in the movie... =p
    X = x;
}

@Override
protected void onHandleIntent(Intent intent) {
    // somehow X is null here after i set him in the constractor. any idea why?
}

如果有人可以编辑我的标题,我不确定我是否写得很好并且清晰易懂...谢谢!

永远不会使用该构造函数。 你的IntentService在别处有一个零参数的MyService构造函数——这将是 Android 用来创建你的服务实例的构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM