如何打算在android /另一个页面上弹出空闲时间的消息? [英] How to intent to another page on android/pop up a message from idle time?

查看:101
本文介绍了如何打算在android /另一个页面上弹出空闲时间的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

光晕,第一个我想知道我的Android应用程序的空闲时间。之后,如果它是空闲时间模式,我会做一些事情。

Halo, the first i want to know the idle time at my android application. after that, i will do something if it is a idle time mode.

我遵循这个链接。
应用程序空闲时间

我的程序正常工作,但突然出现问题。我不能移动到其他页面(例如登录页面)或使用alertdialog弹出一条消息,因为它在线程中。你有什么解决办法?

my program work properly, but suddenly the problem show up. I can't move to the other page (for example to the login page) or pop up a message using alertdialog because its in a thread. Do you have any solutions?

public class ControlActivity extends Activity {
private static final String TAG=ControlActivity.class.getName();

/**
 * Gets reference to global Application
 * @return must always be type of ControlApplication! See AndroidManifest.xml
 */
public ControlApplication getApp()
{
    return (ControlApplication )this.getApplication();
}

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    getApp().touch();
    Log.d(TAG, "User interaction to "+this.toString());

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}

这是我的ControlApplication.java

here is my ControlApplication.java

public class ControlApplication extends Application {
private static final String TAG=ControlApplication.class.getName();
private Waiter waiter;
@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "Starting application"+this.toString());
    //setContentView(R.layout.activity_main);
    waiter=new Waiter(5*60*1000); //5 mins
    waiter.start();
    Toast.makeText(ControlApplication.this, "start", Toast.LENGTH_LONG).show();
}

public void touch()
{
    waiter.touch();
    Toast.makeText(ControlApplication.this, "touch", Toast.LENGTH_LONG).show();
}   }

这里是Waiter.java

here is the Waiter.java

public class Waiter extends Thread implements Runnable{
private static final String TAG=Waiter.class.getName();
private long lastUsed;
private long period;
private boolean stop;
Context activity;

public Waiter(long period)
{
    this.period=period;
    stop=false;
}

@SuppressLint("ParserError")
public void run()
{
    long idle=0; 
    this.touch();
    do
    {
        idle=System.currentTimeMillis()-lastUsed;
        Log.d(TAG, "Application is idle for "+idle +" ms");
        try
        {
            Thread.sleep(5000); //check every 5 seconds
        }
        catch (InterruptedException e)
        {
            Log.d(TAG, "Waiter interrupted!");
        }
        if(idle > period)
        {
            idle=0;
            //do something here - e.g. call popup or so
            //Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
            stopCounter();
        }
    }
    while(!stop);

    Log.d(TAG, "Finishing Waiter thread");
}

public synchronized void touch()
{
    lastUsed=System.currentTimeMillis();
}

public synchronized void forceInterrupt()
{
    this.interrupt();
}

//soft stopping of thread
public synchronized void stopCounter()
{
    stop=true;
}

public synchronized void setPeriod(long period)
{
    this.period=period;
}}

我试图创建一个新类并调用一个方法来实现。它也失败了。尝试从该方法弹出一条消息,它也失败。

I tried to create a new class and call a method to intent. Its also fail. tried to pop up a message from that method its also fail.

你们有空闲时间的其他解决方案吗?谢谢。

do you guys have any other solutions for idle time? thanks.

Regards,
Alfred Angkasa

Regards, Alfred Angkasa

推荐答案

我自己找到答案通过谷歌搜索和尝试5小时..:D

I just found by myself the answer by search on google and try for 5 hours.. :D

我希望我的答案也会帮助你。

I hope my answer will help you too.

首先,我将ControlApplication和Waiter与ControlActivity进行混合。这意味着我不需要这两个文件。我的ControlActivity将扩展活动(如果处于空闲模式,则将其用于我的意图到另一页),并且我将实现runnable(它用于我来运行线程)。

First, I mix the ControlApplication and Waiter with ControlActivity. Thats mean I don't need both files. My ControlActivity will extends the activity (its use for me to intent to the other page if in idle mode), and i will implements runnable(its use for me to run the thread).

之后,我有一个名为onUserInteraction()的方法,只要用户触摸或点击某事,此方法可帮助我获取用户交互。
在onCreate中,我启动所有的变量,包括lastUsed,period和stop。

after that i have a method called onUserInteraction(), this method help me to get the user interaction, whenever the user touch or click something. in the onCreate, i initiate all the variable including lastUsed, period, and stop.

为什么要启动?因为你需要知道你的应用程序处于空闲模式的时间是多少秒。那是周期使用。停止变量用于我每5秒迭代和搜索(您也可以每隔一秒钟检查空闲或不空)我的应用程序空闲或不空闲。我通过调用方法触发启动lastUsed。我将ControlApplication中的touch方法复制到我的ControlActivity中。通过调用touch方法,我可以知道我最后一次使用什么时候。之后,我开始了我的线程。

why should I initiate that? because you need to know how many seconds to know that your apps is on idle mode or not. that was period use. Stop variable is use for me to iterate and searching every 5 seconds(you can also make it every second to check idle or not) my apps is idle or not. I initiate lastUsed by calling method touch. I copied touch method from ControlApplication into my ControlActivity. By calling touch method, I can know when is my lastused. After that I start my thread.

在我的运行方法中,我设置idle = 0并做一些循环来检查。我每5秒钟检查一下我的应用程序是否处于空闲模式。
idle = System.System.currentTimeMillis() - lastUsed - >我用这个来知道空闲是否已经是套用的,而不是使用if方法。
如果空闲时间大于句点,我的应用程序必须处于空闲模式。之后,我停止迭代并使用处理程序来管理它。

in my run method, i set idle = 0. and do some looping to check. i check every 5 seconds to know my apps is on idle mode or not. idle = System.System.currentTimeMillis()-lastUsed -> i used this to know if the idle is already suite with the period or not using if method. if the idle is greater than period, my apps must be in idle mode. after that i stop the iteration and using handler to manage it.

我设置handler.sendEmptyMessage(0),并创建Handler。在处理程序我移动到另一页。
这是我的完整代码。

i set handler.sendEmptyMessage(0), and create Handler. At handler i move to the other page. this is my full code.

public class MainActivity extends Activity implements Runnable {
private static final String TAG= MainActivity.class.getName();
private long lastUsed;
private int period;
private boolean stop;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    period = 10000;
    stop=false;
    touch();
    Thread currentThread = new Thread(this);
    currentThread.start();
    Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    touch();
    Log.d(TAG, "User interaction to "+this.toString());
}

public synchronized void touch()
{
    lastUsed=System.currentTimeMillis();
    Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_SHORT).show();
}

public void moveIntent() {
    Intent intent = new Intent(this, AfterIdle.class);
    startActivity(intent);
}

public void validate(View view) {
    switch (view.getId()) {
        case R.id.button1 :
            Intent intent = new Intent(this, AfterIdle.class);
            startActivity(intent);
            break;
    }
}

@Override
public void run() {
    // TODO Auto-generated method stub
    long idle;

    while (!stop) {
        idle=System.currentTimeMillis()-lastUsed;
        try
        {
            Thread.sleep(5000); //check every 5 seconds
        }
        catch (InterruptedException e)
        {
            Log.d(TAG, "Waiter interrupted!");
        }
        if (idle > period) {
            idle = 0;
            stop = true;
        }
    }
    handler.sendEmptyMessage(0);
}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        moveIntent();
    }
};}

希望这段代码能帮助别人他们有同样的问题,我上次面对。如果我的答案是错误的,我希望有人会为我纠正答案。
谢谢。

I hope this code will help another people if they have the same problem that i faced last time. I wish someone would correct the answer for me if my answer is wrong. thanks.

问候,
阿尔弗雷德·安卡萨

Regards, Alfred Angkasa

这篇关于如何打算在android /另一个页面上弹出空闲时间的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆