简体   繁体   中英

How to keep IOIO connected when android screen goes off?

I am using the following code with IOIO to act as a motion detector, the problem is the IOIO is disconnected whenever my phone screen goes off! I do not want the screen to stay on all the time to keep the IOIO connected!

any solution please?

    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();
}

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM