MyBroadCastReciever.java
public class MyBroadCastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("Check","Screen went OFF");
Toast.makeText(context, "screen OFF",Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("Check","Screen went ON");
Toast.makeText(context, "screen ON",Toast.LENGTH_LONG).show();
}
}
}
MainActivity.java
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
MyBroadCastReciever r = new MyBroadCastReciever();
registerReceiver(r,filter);
IntentFilter filter2 = new IntentFilter(Intent.ACTION_SCREEN_ON);
MyBroadCastReciever r2 = new MyBroadCastReciever();
registerReceiver(r2,filter2);
沒有留言:
張貼留言