通过注册BroadcastReceiver,当设备屏幕被打开/关闭时,我可以获得SCREEN ON和SCREEN OFF toast消息。以同样的方式,当点击设备相机按钮时,我可以获得CAMERA ON和CAMERA OFF Toast消息吗?
我使用以下源代码来获取SCREEN ON / OFF toast消息。
public class MainActivity extends Activity {
BroadcastReceiver mybroadcast = new BroadcastReceiver() {
// When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("[BroadcastReceiver]", "Screen ON");
Toast.makeText(getApplicationContext(), "SCREEN ON",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("[BroadcastReceiver]", "Screen OFF");
Toast.makeText(getApplicationContext(), "SCREEN OFF",Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}