在Android屏幕上5分钟内没有动作的情况下如何播放视频?

时间:2014-11-17 07:24:56

标签: android user-interaction

我希望当我的应用程序退出时,退出后如果屏幕上没有任何操作执行5分钟,则每次都会播放视频,此视频ID在我的应用中定义。 任何帮助,将不胜感激。提前谢谢。

我有以下类,但它没有正常工作,如何制作与服务相同的代码?

public class IdlePhoneState extends Activity {

Handler hl_timeout = new Handler();

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

    try{
        hl_timeout.postDelayed(DoOnTimeOut, 120000); // 2 min
        }catch(Exception e)
        {
            e.printStackTrace();
        }
}

//  Toast
Thread DoOnTimeOut = new Thread() {
    public void run() {
        try{
            Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
};

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    //Remove any previous callback
    try{
    hl_timeout.removeCallbacks(DoOnTimeOut);
    hl_timeout.postDelayed(DoOnTimeOut, 120000);

    System.out.println("ggggggggggggggggggggg");
    Intent intr= new Intent(getApplicationContext(), VideoPlayerActivity.class);
    startActivity(intr);


    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:0)

如下所示。

  1. 您应该在布局的最外层元素上设置ID:

    android:id =“@ + id / entire_view”

  2. 在java文件中,如下所示。

    查看视图= getViewById(R.id.entire_view);

  3. 为该根布局编写touchlistener代码。

  4. view.setOnTouchListener(... 在触摸中保存触摸时间到共享的优先级是这样的。然后将保存的时间与当前时间进行比较。如果差异超过五分钟,则播放视频。

    对于持续时间检查,尝试使用Alarm Manager,否则使用CountDownTimer。