intentservice在生成我的对象后不会保存它

zbsbpyhn  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(313)

我得到了一个wierd情况,我构建的一个对象在方法onhandleint中返回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?
}

如果有人能编辑我的标题,我不确定我写得好,明白。。。谢谢!

gxwragnw

gxwragnw1#

这个构造函数永远不会被使用。你的 IntentService 在其他地方,是零论点 MyService 构造器-这将是android用来创建服务示例的构造器。

相关问题