igal k
igal k

Reputation: 1914

Android - Handle global configuration changes

I'm writing a service that tracks system changes, meaning, I'm willing to track whenever a keyboard becomes visible / hidden for any application.

To achieve the following task, i built a small Activity that launches a services

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);
    }
}

The manifest.xml itself

<?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>

and the service itself:

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");
    }
}

Problem is I'm notified for changes within, and only for my activity. Which is seen as a white screen for unknown reasons(even though i didn't use SetContentView(..))

Upvotes: 2

Views: 572

Answers (1)

David Wasser
David Wasser

Reputation: 95626

When you talk of "keyboard" you are talking about the soft keyboard. This doesn't result in a configuration change, as the configuration has not changed.

There are devices with hardware keyboards that slide out, so they generate a configuration change event when they are slid out or back in again. There is also a possibility to attach an external keyboard to some Android devices and the act of connecting or disconnecting a hardware keyboard also generates a configuration change event.

To detect if the keyboard is shown in your own app, see How to check visibility of software keyboard in Android?

As far as I know, there is no way to find out if the soft keyboard is shown in another app.

Upvotes: 1

Related Questions