calling getSystemService inside a service

后端 未结 1 1754
有刺的猬
有刺的猬 2021-01-26 23:30

I\'m trying to write a service that get the heart rate on Gear Live, following the question here Get Heart Rate from "Sensor" Samsung Gear Live

If I put this p

相关标签:
1条回答
  • 2021-01-27 00:14

    I figured it out, the reason is that inside getSystemService, there's a call to this function:

     @Override
    public Object getSystemService(String name) {
        return mBase.getSystemService(name);
    }
    

    which has mBase as null. This is a Context object. So I had to put the context of the activity that start the service in onCreate(). Code below:

     public HeartRateMonitorService(Context context){
        super();
        mBaseConext = context;
    }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        attachBaseContext(mBaseConext);
    }
    

    I'm not sure this is the optimal solution, but it is a workaround.

    0 讨论(0)
提交回复
热议问题