LayerService クラス
public class LayerService extends Service {
private WindowManager windowManager;
private View view;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Inflater 作成
LayoutInflater layoutInflater = LayoutInflater.from(this);
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// Viewの作成
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, // 横幅
WindowManager.LayoutParams.WRAP_CONTENT, // 高さ
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // 表示レイヤー 優先度
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, // viewの動作(touch等)
PixelFormat.TRANSLUCENT // viewを透明にする
);
view = layoutInflater.inflate(R.layout.overlay, null);
windowManager.addView(view, params); // WindowManagerにViewを追加
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
windowManager.removeView(view); // サービス破棄時Viewを削除する
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
レイアウトファイル overlay.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!--horizontal-->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@color/colorPrimaryDark"
android:text=" Overlay Test " />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonStart_onClick(View view) {
startService(new Intent(MainActivity.this, LayerService.class));
}
public void buttonStop_onClick(View view) {
stopService(new Intent(MainActivity.this, LayerService.class));
}
}
レイアウトファイル activity_main.xml
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonStart_onClick(View view) {
startService(new Intent(MainActivity.this, LayerService.class));
}
public void buttonStop_onClick(View view) {
stopService(new Intent(MainActivity.this, LayerService.class));
}
}
マニフェストファイル AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.shell.overlaytest04">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<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=".LayerService" />
</application>
</manifest>