在Android的后台服务运行监听器 [英] Running listener in a background service in android

查看:160
本文介绍了在Android的后台服务运行监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从每当一些数据写在它的USB串行口接收数据。

有关这一点,我已经创建了具有onread听众从串口读取数据时,数据可用的服务。

我追加在我的活动中接收到的数据一个TextView。但我看不到任何东西。

我的服务正在运行,但不能接收任何数据。

下面是我的服务类code。

 公共类BroadcastService延伸服务{
    私有静态最后字符串变量=BroadcastService;
    公共静态最后弦乐BROADCAST_ACTION =com.example.flowtest.displayevent;
    私人最终处理程序处理程序=新的处理程序();
    // DatabaseHandle DB =新DatabaseHandle(本);
    意向意图;
    INT计数器= 0;
    INT I = 0,标志= 0;
    串D =一个;
    byte []的BUF =新的字节[5];
    byte []的开始=新的字节[21];
    byte []的RBUF; //更新
    字符串str;
    Physicaloid mPhysicaloid =新Physicaloid(本);

    @覆盖
    公共无效的onCreate(){
        super.onCreate();

        意图=新的意图(BROADCAST_ACTION);
        // mPhysicaloid.open();

    }

    @覆盖
    公共无效ONSTART(意向意图,诠释startId){

        mPhysicaloid.open();

        handler.removeCallbacks(的getData);
        handler.post(的getData);
    }

    私人Runnable接口的getData =新的Runnable(){
        公共无效的run(){
            Log.d(服务服务,);
            mPhysicaloid.addReadListener(新ReadLisener(){

                @覆盖
                公共无效onRead(INT尺寸){
                    // TODO自动生成方法存根
                    byte []的RBUF =新的字节[尺寸]
                    mPhysicaloid.read(RBUF,大小);
                    尝试 {
                        海峡=新的String(RBUF,UTF-8);

                        如果(str.equals(空)){
                            标志= 0;
                            intent.putExtra(inputString,没有运气);
                            sendBroadcast(意向);
                        } 其他 {
                            标志= 1;
                            intent.putExtra(inputString,STR);
                            sendBroadcast(意向);
                        }

                        // sendBroadcast(意向);

                    }赶上(UnsupportedEncodingException E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }

                }

            });

        }
    };

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    @覆盖
    公共无效的onDestroy(){
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
    }
}
 

现在我的活动,

 私人的BroadcastReceiver的BroadcastReceiver =新的BroadcastReceiver(){

    @覆盖
     公共无效的onReceive(上下文的背景下,意图意图){
        的updateUI(意向);
    }

    私人无效的updateUI(意向意图){
        // TODO自动生成方法存根
        字符串数据= intent.getStringExtra(inputString)的toString()。
        如果(data.equals(正常)){
            I =新的意图(MainActivity.this,ModeScreenActivity.class);
              startActivity(ⅰ);
              完();
        }
        其他{

        }
        tvAppend(TV1,数据);
    }
};
 

解决方案

您设置您读侦听器内的getData可运行。这意味着,虽然的getData运行侦听才会有效。由于的getData立即终止,听者将永远不会得到执行的机会。

上移听众了getData或具有的getData是一个长期运行的线程具有相同的寿命为您服务。

I am needed to receive data from usb serial port whenever some data is written on it.

For this i have created a service that has a onread listener which reads data from serial port when data is available.

I am appending a textview with the received data in my activity. But i cannot see anything.

My service is running but not able to receive any data.

Below is my service class code.

public class BroadcastService extends Service {
    private static final String TAG = "BroadcastService";
    public static final String BROADCAST_ACTION = "com.example.flowtest.displayevent";
    private final Handler handler = new Handler();
    // DatabaseHandle db = new DatabaseHandle(this);
    Intent intent;
    int counter = 0;
    int i = 0, flag = 0;
    String d = "a";
    byte[] buf = new byte[5];
    byte[] start = new byte[21];
    byte[] rbuf;// updated
    String str;
    Physicaloid mPhysicaloid = new Physicaloid(this);

    @Override
    public void onCreate() {
        super.onCreate();

        intent = new Intent(BROADCAST_ACTION);
        // mPhysicaloid.open();

    }

    @Override
    public void onStart(Intent intent, int startId) {

        mPhysicaloid.open();

        handler.removeCallbacks(getdata);
        handler.post(getdata);
    }

    private Runnable getdata = new Runnable() {
        public void run() {
            Log.d("service", "in service");
            mPhysicaloid.addReadListener(new ReadLisener() {

                @Override
                public void onRead(int size) {
                    // TODO Auto-generated method stub
                    byte[] rbuf = new byte[size];
                    mPhysicaloid.read(rbuf, size);
                    try {
                        str = new String(rbuf, "UTF-8");

                        if (str.equals(null)) {
                            flag = 0;
                            intent.putExtra("inputString", "no luck");
                            sendBroadcast(intent);
                        } else {
                            flag = 1;
                            intent.putExtra("inputString", str);
                            sendBroadcast(intent);
                        }

                        // sendBroadcast(intent);

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            });

        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        // handler.removeCallbacks(sendUpdatesToUI);
        super.onDestroy();
    }
}

Now my activity ,

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
     public void onReceive(Context context, Intent intent) {
        updateUI(intent);       
    }

    private void updateUI(Intent intent) {
        // TODO Auto-generated method stub
        String data = intent.getStringExtra("inputString").toString();
        if(data.equals("NORMAL")){
            i = new Intent(MainActivity.this,ModeScreenActivity.class);
              startActivity(i);
              finish();
        }
        else{

        }
        tvAppend(tv1,data);
    }
}; 

解决方案

You setup your read listener within the getData runnable. This means that the listener will only be valid while getData is running. Since getData terminates immediately, the listener will never get a chance to execute.

Either move the listener out of getData or have getData be a long running thread with the same lifetime as your service.

这篇关于在Android的后台服务运行监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆