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

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

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5600802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存