如何使用服务监控Android中的方向更改

时间:2011-01-07 11:46:00

标签: android service android-widget screen-orientation

我正在编写一个显示倒数计时器的小工具。我把小部件按照我想要的方式工作,直到我将手机从横向翻转到肖像。我的小部件不会更新并在小部件启动时进入初始状态,直到我的定期警报调用onupdate。一旦方向改变以更新我的小部件,我想手动调用onupdate我已经研究了一段时间了,我发现我需要使用一个服务来监控方向变化并调用我的onupdate我的小部件。

我的问题是我无法找到关于如何使用服务来监控变化的直接答案。我已经看到,通过一项活动,我可以将 android:configChanges =“orientation | keyboardHidden”添加到活动的清单中,并使用 onConfigurationChanged ,但我可以这样做吗为了服务。如果是这样的话?是否有更好的方法来监控方向变化?我还在互联网上看到,服务也不是最好的方法。

我见过用于创建方向侦听器的教程,但它们似乎使用折旧函数调用。

先谢谢

4 个答案:

答案 0 :(得分:26)

Service#onConfigurationChanged(Configuration newConfig)适合我。无需为我的意见注册接收器。我也没有为AndroidManifest添加任何过滤器。由于没有人提出这个解决方案,我在讨论中是否遗漏了什我的服务正在前台运行。

只需放入您的服务:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //Your handling
}

答案 1 :(得分:13)

请在下面找到一个符合您要求的示例,希望这会有所帮助。

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>

答案 2 :(得分:2)

你可以创建一个监听BroadcastReceiverIntent.ACTION_CONFIGURATION_CHANGED(android.intent.action.CONFIGURATION_CHANGED)

请注意它确实说:

  

你不能通过这个来接收   仅在清单中声明的​​组件   通过明确注册它   Context.registerReceiver()。

这意味着您无法在清单文件中注册接收器。你必须在代码中注册它。

然后得到配置并检查Floern所说的方向。

答案 3 :(得分:1)

我建议您使用OrientationEventListener对象。它完全符合您的要求而且不会弃用。它自API级别3以来一直存在。