gpt4 book ai didi

android - 扩展 Activity 以实现空闲计时器

转载 作者:太空狗 更新时间:2023-10-29 15:22:53 25 4
gpt4 key购买 nike

我已经开发了一些实现计时器的代码 - 它每秒滴答一次,并且在不触摸显示器 x 秒后启动“空闲” Activity 。但是,这需要为大约 8 个 Activity 实现...我可以将相关代码复制并粘贴到每个 Activity ,但这不是一个优雅的解决方案。

因此,我开始创建我的自定义 Activity “TimedActivity”来扩展 Activity。然后我需要实现的其他 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);
}
}
}

使用它的后续 Activity 将使用:

public class ActivitySelect extends TimedActivity
{
}

View 的 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);
}

}

}

要使用这个 ActivityTimed,在你启动的 Activity 中做:

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!
}
}

最佳答案

编辑:好吧,我之前试图提出的建议并不完全是我的意思......

ActivitySelectonCreate() 方法中(例如)放入代码...

setContentView(/* Id of layout file for ActivitySelect */);
mLinearLayoutMain = (LinearLayout) findViewById(/* Id of LinearLayout for ActivitySelect */);

这意味着 ActivitySelect 将设置其内容 View 并将继承的 mLinearLayoutMain 成员设置到 View 。

onStart() 中为 TimedActivity 放...

mLinearLayoutMain.setOnTouchListener(this);

您可能还想在 TimedActivity 中的 onTouch() 方法上使用 @Override

关于android - 扩展 Activity 以实现空闲计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5822521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com