栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android-计算电源按钮的点击次数并启动活动

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android-计算电源按钮的点击次数并启动活动

试试这个,

public class MyReceiver extends BroadcastReceiver {    static int countPowerOff=0;    private Activity activity=null;    public MyReceiver (Activity activity)    {    this.activity=activity;    }    @Override    public void onReceive(Context context, Intent intent) {      Log.v("onReceive", "Power button is pressed.");      Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)  .show();     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {    countPowerOff++;    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {      if(countPowerOff==5)      {          Intent i =new Intent(activity,NewActivity.class);          activity.startActivity(i);       }    }}

和,

public class MainActivity extends Activity {        @Override        public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); MyReceiver mReceiver = new MyReceiver (this); registerReceiver(mReceiver, filter);        }        @Override        public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595503.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号