Ниже приведен пример, который делает то, что вы просите, надеюсь, это поможет.
public class ScreenOrientationListener extends Activity {
private static final String TAG = "ScreenOrientationListener";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
startService( new Intent(this, MyService.class) );
}
}
и вот идет класс MyService
public class MyService extends Service {
private static final String TAG = "MyService";
private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED";
private static Context mContext;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate()");
mContext = this;
IntentFilter filter = new IntentFilter();
filter.addAction(BCAST_CONFIGCHANGED);
this.registerReceiver(mBroadcastReceiver, filter);
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()");
//Unregister receiver to avoid memory leaks
mContext.unregisterReceiver(mBroadcastReceiver);
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart()");
}
public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent myIntent) {
if ( myIntent.getAction().equals( BCAST_CONFIGCHANGED ) ) {
Log.d(TAG, "received->" + BCAST_CONFIGCHANGED);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
// it's Landscape
Log.d(TAG, "LANDSCAPE");
}
else {
Log.d(TAG, "PORTRAIT");
}
}
}
};
}
, и вот часть для определения MyServiceв файле манифеста
<!-- Services -->
<service android:enabled="true" android:name="com.wareninja.android.external.screenorientationlistener.services.MyService">
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED"/>
</intent-filter>
</service>