扩展活动以实现空闲计时器

时间:2011-04-28 17:18:18

标签: android

我已经开发了一些实现计时器的代码 - 它每秒都会打勾,在没有触摸显示屏的x秒之后启动“空闲”活动。 但是,这需要在大约8个活动中实现......我可以将相关代码复制并粘贴到每个活动中,但这不是一个优雅的解决方案。

所以,我开始创建自定义活动“TimedActivity”来扩展Activity。我需要实现的其他活动可以实现“TimedActivity”。

但是,我的TimedActivity如何检测onTouch事件?

这是我到目前为止所做的:

public class TimedActivity extends Activity implements OnTouchListener
{
    private LinearLayout mLinearLayoutMain;//intended to be the root node in the XML
    private Timer mTimerSeconds;
    private int mIntIdleSeconds;

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

        mIntIdleSeconds=0;

        mTimerSeconds = new Timer();
        mTimerSeconds.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                timerSecondsCounter();
            }
        }, 0, 1000);

        //the whole screen should become sensitive to touch
        //HOWEVER, there's no View established
        mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
        //mLinearLayoutMain remains null, so next line excepts
        mLinearLayoutMain.setOnTouchListener(this);
    }


    @Override
    protected void onDestroy()
    {
        if (mTimerSeconds != null)
        {
            mTimerSeconds.cancel();
        }
        super.onDestroy();
    }


    public boolean onTouch(View v, MotionEvent event)
    {
        mIntIdleSeconds=0;
        return false;//do not consume
    }

    private void timerSecondsCounter()
    {
        mIntIdleSeconds++;

        if (mIntIdleSeconds == Constants.MAX_IDLE_TIME_SECONDS)
        {
            final Intent intent = new Intent(this, what.ever.com.Idle.class);
            startActivity(intent);
        }
    }
}

随后使用它的活动将使用:

public class ActivitySelect extends TimedActivity
{
}

并且视图的xml将是:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main">

<!-- MORE STUFF --->

</LinearLayout>

我觉得我很亲密,但是我需要做些什么才能让它发挥作用?

修改: 这就是我最终实施的内容......

public class TimedActivity extends Activity
{
    //member variables
    private Timer mTimerSeconds;
    private int mIntIdleSeconds;
    private boolean mBoolInitialized=false;

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

    @Override
    protected void onDestroy()
    {
        if (mTimerSeconds != null)
        {
            mTimerSeconds.cancel();
        }
        super.onDestroy();
    }

    public void onTouch()
    {
        mIntIdleSeconds=0;
    }

    /** start the idle timer */
    public void startIdleTimer()
    {
        if (mBoolInitialized == false)
        {
            mBoolInitialized = true;

            //initialize idle counter
            mIntIdleSeconds=0;

            //create timer to tick every second
            mTimerSeconds = new Timer();
            mTimerSeconds.schedule(new TimerTask()
            {
                @Override
                public void run()
                {
                    timerSecondsCounter();
                }
            }, 0, 1000);
        }
    }

    /** called every second to count idle time and to update clock on Welcome screen */
    private void timerSecondsCounter()
    {

        mIntIdleSeconds++;

        if (mIntIdleSeconds == Constants.MAX_IDLE_TIME_SECONDS)
        {
            //idle time long enough to launch standby activity
            final Intent intent = new Intent(this, what.ever.com.Idle.class);
            startActivity(intent);
        }

    }

}

要在启动的Activity中使用此ActivityTimed,请执行以下操作:

public class ActivitySelect extends TimedActivity implements OnTouchListener
{
    //UI references
    private LinearLayout mLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);//triggers TimedActivity::onCreate()
        setContentView(R.layout.select);

        //get references to all of your widgets
        mLinearLayout = (LinearLayout) findViewById(R.id.select_linearlayout_main);

        //set your widgets as touchable
        mLinearLayout.setOnTouchListener(this);

        //start the idle timer
        super.startIdleTimer();
    }

    @Override
    protected void onDestroy()
    {
        //this is very important here ;-)
        super.onDestroy();
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        final int actionPerformed = event.getAction();

        //reset idle timer
        // put this here so that the touching of empty space is captured too
        //  it seems that LinearLayout doesn't trigger a MotionEvent.ACTION_UP or MotionEvent.ACTION_MOVE
        if (actionPerformed == MotionEvent.ACTION_DOWN)
        {
            super.onTouch();
        }

        return false;//do not consume event!
    }
}

1 个答案:

答案 0 :(得分:0)

编辑:好的,我之前尝试建议的并不是我的意思......

onCreate()的{​​{1}}方法(例如)中输入代码......

ActivitySelect

这意味着setContentView(/* Id of layout file for ActivitySelect */); mLinearLayoutMain = (LinearLayout) findViewById(/* Id of LinearLayout for ActivitySelect */); 将设置其内容视图,并将继承的ActivitySelect成员设置为视图。

mLinearLayoutMain代表onStart() ...

TimedActivity

您可能还想在mLinearLayoutMain.setOnTouchListener(this);

中的@Override方法上使用onTouch()
相关问题