试试这个,
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; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)