如何意图另一页上的Andr​​oid /弹出的空闲时间信息?弹出、意图、时间、信息

2023-09-08 08:48:57 作者:米老鼠小姐ゝ

晕,首先我想知道在我的Andr​​oid应用程序的空闲时间。在这之后,我会做一些事情,如果它是一个空闲时间模式。

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.

问候,阿尔弗雷德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和服务员与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,周期和停止。

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摸法进我ControlActivity。通过调用触摸的方法,我知道是我的LASTUSED时。从那以后,我开始我的线程。

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.

在我的run方法,我设置空闲= 0,并做一些循环检查。我检查每5秒钟就知道我的应用程序是在空闲模式与否。空闲= System.System.currentTimeMillis() - LASTUSED - >我用这个知道,如果闲置已经套房设有期限,或者如果方法不使用。如果闲置超过一段时,我的应用程序必须处于空闲模式。从那以后,我停止迭代,并使用处理程序来管理它。

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),并创建处理程序。在处理我移动到另一个页面。这是我的全code。

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

我希望这code将帮助其他人,如果他们有我面对上一次同样的问题。我希望有人能正确答案对我来说,如果我的答案是错误的。谢谢。

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.

问候,阿尔弗雷德Angkasa

Regards, Alfred Angkasa

 
精彩推荐
图片推荐