0

我将以下代码与 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();
}

}

4

2 回答 2

0

Option 1) Lock the screen so it always stays awake

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

Option 2) Fix your response to onPause().

When the screen goes off the onPause() method is called and you should handle it as otherwise your activity will be closed.

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

The onPause() normally calls the ioio.disconnect() so this should be overrode.

于 2014-10-01T15:43:47.617 回答
0

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

于 2015-01-27T04:43:51.023 回答