0

我正在android中编写一个本地绑定的服务。我的应用程序有两个类,即。活动和服务

在我的活动中,我有一个 TextView 和一个按钮。我在活动的 onCreate() 方法中启动和绑定服务。单击按钮时,我希望使用从 Service 类中编写的函数返回的 String 值更新我的 TextView

但是我总是将 null 作为返回值。可能是什么问题?

我在这里给出我的整个代码

这是活动课

 package com.tcs.cto.healthcare;

 import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tcs.cto.healthcare.GlucoseDataService.LocalUSBSerialBinder;


public class HealthcareCTOActivity extends Activity {

Button getDataButton;
TextView dataView;

String TAG="HealthCareCTO";
String glucoseData;
 GlucoseDataService myService;
    boolean isBound = false;


    private ServiceConnection myConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalUSBSerialBinder binder = (LocalUSBSerialBinder) service;
            myService = binder.getService();
            isBound = true;
        }

        public void onServiceDisconnected(ComponentName arg0) {
            isBound = false;
        }

       };


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

    getDataButton=(Button)findViewById(R.id.button1);
    dataView=(TextView)findViewById(R.id.glucoseDataView);
    Intent intent = new Intent(this, GlucoseDataService.class);
    startService(intent);
    bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    Log.i(TAG,"Glucose Service Started");

    //getDataButton=(Button)findViewById(R.id.button1);
    //dataView=(TextView)findViewById(R.id.glucoseDataView);
    getDataButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Log.i(TAG,"Button clicked");
            try{
                //glucoseData = myService.getGlucoseValueOverSerialPort();
                String currentTime="XXX";
                currentTime = myService.getCurrentTime();
                //glucoseData = myService.getGlucoseValueOverSerialPort();
                //Log.i(TAG,"glucoseData --> "+glucoseData);
                Log.i(TAG,"currentTume --> "+currentTime);
                dataView.setText(currentTime);
            }catch(Exception ex){
                Log.i(TAG,"Button click exception");
                ex.printStackTrace();
            }


        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_healthcare_cto, menu);
    return true;
}

}

我的服务类如下

package com.tcs.cto.healthcare;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class GlucoseDataService extends Service {

private final IBinder myBinder = new LocalUSBSerialBinder();
String glucoseData="0000";
String TAG="GlucoseService";
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}


 public String getGlucoseValueOverSerialPort(){

     int glucoseValue=120;
    glucoseData=String.valueOf(glucoseValue);
    return glucoseData;
 }
 public String getCurrentTime() {
     Log.i(TAG,"getCurrentTime");
        SimpleDateFormat dateformat = 
                 new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
        return (dateformat.format(new Date()));
    }

public class LocalUSBSerialBinder extends Binder {
    GlucoseDataService getService() {
        return GlucoseDataService.this;
    }

}; }

4

1 回答 1

0

通过为我尝试连接的设备添加供应商 ID 和 USB 串行驱动程序产品解决了该问题。由于缺少设备 ID,连接对象未创建并返回 null。

 <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
<usb-device vendor-id="4292" product-id="60000" />

将此添加到 device_filter.xml 并更新 android.manifest 文件中的元数据标记后,问题得以解决。

于 2015-01-07T10:52:21.887 回答