如何使用服务来监视Android的取向变化取向、如何使用、Android

由网友(恨不能相逢)分享简介:我正在写一个小工具,将显示一个倒数计时器。我有小部件的工作我想它,直到我翻盖手机从横向到纵向的方式。我的窗口小部件不更新,去给它的,直到的onupdate称为我反复出现报警的小部件开始初始状态。我想手动调用的onupdate一旦方向改变来更新我的插件,我一直在研究,现在这一段时间,我发现我需要使用服务将监视的方向变化,...

我正在写一个小工具,将显示一个倒数计时器。我有小部件的工作我想它,直到我翻盖手机从横向到纵向的方式。我的窗口小部件不更新,去给它的,直到的onupdate称为我反复出现报警的小部件开始初始状态。我想手动调用的onupdate一旦方向改变来更新我的插件,我一直在研究,现在这一段时间,我发现我需要使用服务将监视的方向变化,并打电话给我的onupdate的我的小部件。

I'm writing a Widget that will display a countdown timer. I have the widget working the way I want it until I flip the phone from landscape to portrait. My widget does not update and goes to it's initial state at start of the widget until an onupdate is called by my recurring alarm. I would like to call an onupdate manually once the orientation changes to update my widget I've been researching this for a while now and I've found out that I need to use a Service which will monitor the orientation changes and call my onupdate for my widget.

我的问题是我找不到一个直接的答案,如何使用服务来监视变化。我见过有一个活动,我可以添加的安卓configChanges =方向| keyboardHidden应用于清单的活动和使用的 onConfigurationChanged 的,但我可以做到这一点为服务。如果是的话怎么样?有没有更好的方法来监测方向变化?我也看到了一个服务是不是要么做到这一点的最好方式在互联网上。

My problem is I can't find a straight answer as to how to use a service to monitor the change. I've seen that with an activity I can add android:configChanges="orientation|keyboardHidden" to the manifest for an activity and use a onConfigurationChanged, but can I do this for a service. If so how? Is there a better way to monitor the orientation change? I've also read on the internet that a service isn't the best way to do this either.

我见过的教程创建定位监听器,但他们似乎用德preciated函数调用。

I've seen tutorials for creating orientation listeners but they seem to use depreciated function calls.

在此先感谢

推荐答案

请在下面的示例中找到它做你的要求,希望这有助于。

Please find below an example which does what you ask for, hope this helps.

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类

and here comes MyService class

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清单文件

and here is the part to define MyService in manifest file

<!-- 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>
阅读全文

相关推荐

最新文章