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

Dys*_*sen 18 service android android-widget screen-orientation

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

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

我已经看过用于创建方向监听器的教程,但它们似乎使用折旧函数调用.

提前致谢

One*_*rld 27

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

只需放入您的服务:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //Your handling
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,这有效并且比注册广播意图简单得多! (2认同)

gul*_*yuz 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) );
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是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");
                }
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

这是在清单文件中定义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>
Run Code Online (Sandbox Code Playgroud)


Rya*_*rad 2

您可以创建一个监听Intent.ACTION_CONFIGURATION_CHANGED的BroadcastReceiver (android.intent.action.CONFIGURATION_CHANGED)

请注意,它确实说:

您无法通过清单中声明的​​组件接收此信息,只能通过使用 Context.registerReceiver() 显式注册它。

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

然后获取配置并检查 Floern 所说的方向。