长期运行的ADK附件(服务?) [英] Long running ADK Accessory (Service?)

查看:119
本文介绍了长期运行的ADK附件(服务?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间试图让android ADK连接在服务中运行,而不是在活动中.....有人知道它甚至可能吗?

I've spent days trying to get the android ADK connection running in a service rather then activity..... Anyone know if its even possible?

I希望让该服务处理inputStream和outputStream,以便我可以在后台长时间读取Arduino。

I would like to have the service handled inputStream and outputStream so I can read my Arduino for extended periods in the background.

当活动返回焦点时,我将与该服务绑定并更新GUI。如果可能的话,我最终希望使用服务中的实时数据更新网站,以进行远程监控。

When the activity returns to focus I will bind with the service and update the GUI. If this is possible I would eventually like to update a website with the live data from the service for remote monitoring.

如果能得到帮助,可以提供任何帮助。我是编程的新手,似乎找不到太多有关此主题的信息。

Any help if appreciated. I'm new to programming and can't seem to find much info on this topic.

感谢您的协助。

推荐答案

能够以以下方式运行ADK连接(不是完整的代码。仅是基本的构建块):

I was able to get a ADK connection running in the following way (not complete code. Only the basic building blocks):

首先,我有一个活动,接收adk意图广播( android系统服务基于adk元数据和清单)。

First I have an activity that the receives adk intent broadcasts (android system service bases on the adk meta data and the manifest).

private static final String USB_ACCESSORY_ATTACHED = "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getIntent().getAction() != null && getIntent().getAction().equals(USB_ACCESSORY_ATTACHED)) {
        Intent service = new Intent(this, ADKservice.class);
        service.putExtras(getIntent());
        startService(service);

        Intent launch = new Intent(this, MainActivity.class);
        launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(launch);
    }
    this.finish();
}

如果意图与adk字符串匹配,它将启动adk服务并传递服务的意图信息,启动用户界面活动并完成自身。

If the intent matches the adk string it will start the adk service and pass the intent information to the service, launch the user interface activity and finish itself.

用户界面(MainActivity)现在像其他任何服务一样绑定到该服务,因此它可以调用公共方法和/或通过服务回调接收数据(也可以使用本地广播)。

The user interface (MainActivity) now binds to the service just like any other service so it can call public methods and/or receive data via the service callbacks (local broadcast's can also be used).

ADKservice扩展了Runnable来监视USB连接。它还为adk断开连接注册了一个接收器,以便在设备断开连接时可以停止:

The ADKservice extends Runnable to monitor the usb connection. It also registers a receiver for adk disconnect so it can stop if the device gets disconnected:

@Override
public void onCreate() {

    IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
    registerReceiver(mUsbReceiver, filter);


    mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

     mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("ADK Service")
    .setContentText("Started");
    startForeground(notifyID, mBuilder.build());

    super.onCreate();
}

在onCreate完成后,该服务将调用onStartCommand,在此开始adk初始化。

After onCreate has finished the service will call onStartCommand where the adk initialization starts.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartup " + mAccessory );

        mAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
        if (mAccessory != null) {
            openAccessory(mAccessory);              
        }

        return super.onStartCommand(intent, flags, startId);
    }

private void openAccessory(UsbAccessory accessory) {
    Log.d(TAG, "openAccessory: " + accessory);
    UsbManager mUsbManager = (UsbManager) getApplicationContext().getSystemService(Context.USB_SERVICE);
    mFileDescriptor = mUsbManager.openAccessory(accessory);
    if (mFileDescriptor != null) {
        FileDescriptor fd = mFileDescriptor.getFileDescriptor();
        mInputStream = new FileInputStream(fd);
        mOutputStream = new FileOutputStream(fd);
        thread = new Thread(null, this, "ADKserviceThread");
        thread.start(); // start runnable
    }

public void run() {
 // handle adk "usb" messages here
}

    @Override
public void onDestroy() {
    closeAccessory();

    stopForeground(true);
    super.onDestroy();
}

private void closeAccessory() {
    try {
        if (mFileDescriptor != null) {
            mFileDescriptor.close();
        }
    } catch (IOException e) {
    } finally {
        mFileDescriptor = null;
        mAccessory = null;
    }
}

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                closeAccessory();

            stopSelf();


            }
        }
    };

对连接的处理可能需要一些调整,但总体概念似乎可行。希望对大家有帮助!现在看来很容易,但是花了我很长时间(我对编程一直很陌生)

The handling of the connection might need some tweaks but the overall concept seems to work. I hope that helps everyone! It seems easy now but it took me a long time to here (I'm repetitively new to programming)

这篇关于长期运行的ADK附件(服务?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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