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

时间:2016-03-27 21:54:13

标签: java android intentservice bluetooth-lowenergy

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

MyActivity

@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);
}

为MyService

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?
}

如果有人可以编辑我的标题,我不确定我是否写得很清楚,明白了...谢谢!

1 个答案:

答案 0 :(得分:1)

永远不会使用该构造函数。您的IntentService在其他地方有一个零参数MyService构造函数 - 它将是Android用于创建服务实例的构造函数。