-
-
Save was0107/7985861 to your computer and use it in GitHub Desktop.
接收ReceiveBroadcast的视图
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cyrilmottier.android.androidtips; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.content.res.Configuration; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* @author Cyril Mottier | |
*/ | |
public class ReceiveBroadcastView extends View { | |
private static final boolean IS_PRE_FROYO = Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO; | |
public ReceiveBroadcastView(Context context) { | |
super(context); | |
} | |
public ReceiveBroadcastView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ReceiveBroadcastView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
if (IS_PRE_FROYO) { | |
getContext().registerReceiver(mConfigurationChangedReceiver, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED)); | |
} | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
if (IS_PRE_FROYO) { | |
getContext().unregisterReceiver(mConfigurationChangedReceiver); | |
} | |
super.onDetachedFromWindow(); | |
} | |
private BroadcastReceiver mConfigurationChangedReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
onConfigurationChangedSupport(context.getResources().getConfiguration()); | |
} | |
}; | |
@Override | |
protected void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
onConfigurationChangedSupport(newConfig); | |
}; | |
private void onConfigurationChangedSupport(Configuration newConfig) { | |
// This is where you put your code | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment