安卓屏幕关闭时如何保持IOIO连接?

时间:2014-09-08 18:25:43

标签: java android usb disconnected ioio

我使用以下代码与IOIO充当运动检测器,问题是每当我的手机屏幕关闭时IOIO都会断开连接!我不希望屏幕始终保持连接以保持IOIO连接!

请问任何解决方案?

    package com.LookHin.ioio_pir_motion_sensor;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.graphics.Color;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends IOIOActivity {

private ToggleButton toggleButton1;
private TextView textView1;

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

    textView1 = (TextView) findViewById(R.id.textView1);
    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.action_about:
        //Toast.makeText(getApplicationContext(), "Show About", Toast.LENGTH_SHORT).show();

        Intent about = new Intent(this, AboutActivity.class);
        startActivity(about);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
} 


class Looper extends BaseIOIOLooper {

    private DigitalOutput digital_led0;
    private AnalogInput deigital_input;
    int i = 0;

    private float InputStatus;

    @Override
    protected void setup() throws ConnectionLostException {

        digital_led0 = ioio_.openDigitalOutput(0,true);
        deigital_input = ioio_.openAnalogInput(45);

        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), "IOIO Connect", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void loop() throws ConnectionLostException {


        try{
            digital_led0.write(!toggleButton1.isChecked());

            InputStatus = deigital_input.getVoltage();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    textView1.setText(String.format("%.02f",InputStatus)+" v.");

                    if(InputStatus >= 3.0){
                        textView1.setBackgroundColor(Color.RED);

                        if (i == 0){
                        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                        r.play();
                        i = 1;
                        };

                    }else{
                        textView1.setBackgroundColor(Color.TRANSPARENT);
                        i = 0;
                    }

                }
            });

            Thread.sleep(100);

        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}


@Override
protected IOIOLooper createIOIOLooper() {
    return new Looper();
}

}

2 个答案:

答案 0 :(得分:0)

选项1)锁定屏幕以使其始终保持清醒状态

public void onCreate(Bundle savedInstanceState) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

选项2)修复对onPause()的响应。

当屏幕熄灭时,会调用onPause()方法,您应该处理它,否则您的活动将被关闭。

@Override
protected void onPause() {
    // Your code
    super.onPause();
}

onPause()通常调用ioio.disconnect(),所以这应该是overrode。

答案 1 :(得分:0)

IOIOService即使应用程序进入后台,您也可以在后台运行代码。