简体   繁体   English

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

[英]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! 我将以下代码与IOIO一起用作运动检测器,问题是每当手机屏幕关闭时,IOIO都将断开连接! I do not want the screen to stay on all the time to keep the IOIO connected! 我不希望屏幕一直保持连接IOIO的状态!

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 选项1)锁定屏幕,使其始终保持清醒状态

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

Option 2) Fix your response to onPause(). 选项2)将您的响应固定为onPause()。

When the screen goes off the onPause() method is called and you should handle it as otherwise your activity will be closed. 当屏幕关闭时,将调用onPause()方法,您应该对其进行处理,否则您的活动将被关闭。

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

The onPause() normally calls the ioio.disconnect() so this should be overrode. onPause()通常会调用ioio.disconnect(),因此应该覆盖。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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