当我通过主页按钮停止活动时开始一项活动

时间:2017-11-09 00:37:38

标签: java android android-studio android-activity android-homebutton

我尝试开发Android应用程序,当在主要活动中点击主页按钮3次时,启动另一个活动。但这不适合我。

这是我的代码

    HomeWatcher mHomeWatcher = new HomeWatcher(this);
    mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
        @Override
        public void onHomePressed() {
            T++; // to track the clicks
            if (T==3){
            Toast.makeText(getBaseContext(), "Home button pressed", Toast.LENGTH_LONG).show(); }
                startActivity( new Intent (getBaseContext(),Main2Activity.class));}

            // do something here...
        }

    });
    mHomeWatcher.startWatch();
     }

@Override
public void onStop() {


        //Toast.makeText(this, "onstop",Toast.LENGTH_LONG).show();
    if ( T==3 ){
        startActivity( new Intent (getBaseContext(),Main2Activity.class));}
    super.onStop();

}

0 个答案:

没有答案