【问题标题】:Android - Handle global configuration changesAndroid - 处理全局配置更改
【发布时间】:2017-09-18 09:42:34
【问题描述】:

我正在编写一个跟踪系统变化的服务,也就是说,我愿意跟踪键盘何时变为可见/隐藏对于任何应用程序

为了完成以下任务,我构建了一个启动服务的小型 Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);

        //setContentView(R.layout.activity_main);
    }
}

manifest.xml 本身

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="somepackage">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            android:configChanges="orientation|screenSize|keyboardHidden"></service>
    </application>

</manifest>

以及服务本身:

public class MyService extends Service {

    public MyService() {
    }

    private static final String TAG =
            "abbeyservice";

    @Override
    public void onCreate() {
        Log.d(TAG, "Service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "Service onStartCommand");

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        Log.i(TAG, "Service onBind");
        return null;
    }

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

    @Override
    public void onDestroy() {
        Log.i(TAG, "Service onDestroy");
    }
}

问题是我会收到内部更改的通知,并且仅针对我的活动。由于未知原因,它被视为白屏(即使我没有使用 SetContentView(..))

【问题讨论】:

  • 出于未知原因(即使我没有使用 SetContentView(..)就是原因
  • android:configChanges 不是&lt;service&gt; 的有效清单属性。这被忽略了。你的 IDE 应该已经告诉你了。
  • 1.当我的白色 Activity 旋转时,我确实会收到通知 2. 我如何收到有关键盘更改的通知?

标签: java android android-activity android-service


【解决方案1】:

当您谈论“键盘”时,您指的是软键盘。这不会导致配置更改,因为配置没有更改。

有些设备带有滑出的硬件键盘,因此当它们滑出或再次滑入时会生成配置更改事件。还可以将外部键盘连接到某些 Android 设备,并且连接或断开硬件键盘的行为也会生成配置更改事件。

要检测键盘是否显示在您自己的应用中,请参阅 How to check visibility of software keyboard in Android?

据我所知,没有办法知道软键盘是否显示在另一个应用程序中。

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多