2017-09-18 44 views
2

我正在编写一个跟踪系统更改的服务,意思是说,只要键盘变得可见/隐藏,任何应用程序都可以隐藏Android - 处理全局配置更改

要实现以下任务,我建立了一个启动一个服务小活动

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(..))

+3

*由于未知原因(即使我没有使用SetContentView(..)* *是*原因 –

+0

android:configChanges'不是''的有效清单属性。应该已经告诉过你了 –

+0

1.我的白色活动轮换时会收到通知 2.我如何得到有关键盘更改的通知? –

回答

1

当你谈到“键盘”时,你正在谈论软键盘。这不会导致配置更改,因为配置没有更改。

有一些带有硬件键盘的设备会滑出,因此当它们滑出或重新插入时,它们会生成配置更改事件。还可以将外部键盘连接到某些Android设备,连接或断开硬件键盘的操作也会生成配置更改事件。

要检测你自己的应用程序显示的键盘,看 How to check visibility of software keyboard in Android?

据我所知,有没有办法找出是否在另一个应用显示的软键盘。