###前提・実現したいこと
ブルーライトカット的なアプリを作りたいのですが、アプリを終了したら、ブルーライトが消えてしまいます。
スマホが起動中ずっと動かしていたいです。
###ソースコード
java
1public class MainActivity extends Activity{ 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 8 9 final Intent intent = new Intent(MainActivity.this, FilterService.class); 10 11 Button buttonAddFilter = (Button) findViewById(R.id.ButtonAddFilter); 12 buttonAddFilter.setOnClickListener(new View.OnClickListener() { 13 @Override 14 public void onClick(View v) { 15 setNotification(); 16 startService(intent); 17 } 18 }); 19 20 Button buttonRemoveFilter = (Button) findViewById(R.id.ButtonRemoveFilter); 21 buttonRemoveFilter.setOnClickListener(new View.OnClickListener() { 22 @Override 23 public void onClick(View v) { 24 cancelNotification(); 25 stopService(intent); 26 } 27 }); 28 } 29 30 public void setNotification(){ 31 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 32 Intent notificationIntent = new Intent(this, MainActivity.class); 33 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 34 35 Notification notification = new NotificationCompat.Builder(this) 36 .setSmallIcon(R.drawable.ic_launcher) // アイコン 37 .setTicker("BlueScreenが起動しています。") // 通知バーに表示する簡易メッセージ 38 .setWhen(System.currentTimeMillis()) // 時間 39 .setContentTitle("BlueScreen起動中") // 展開メッセージのタイトル 40 .setContentText("てすてす") // 展開メッセージの詳細メッセージ 41 .setContentIntent(contentIntent) // PendingIntent 42 .build(); 43 44 notification.flags = Notification.FLAG_ONGOING_EVENT; 45 notificationManager.notify(1, notification); 46 47 } 48 private void cancelNotification() { 49 NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 50 notificationManager.cancel(R.string.app_name); 51 } 52} 53
###ソースコード
Java
1public class FilterService extends Service { 2 3 private View mView; 4 private WindowManager mWindowManager; 5 private Timer mTimer = null; 6 Handler mHandler = new Handler(); 7 8 @Override 9 public int onStartCommand(Intent intent, int flags, int startId) { 10 11 final LayoutInflater layoutInflater = LayoutInflater.from(this); 12 //重ねあわせするViewの設定 13 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( 14 WindowManager.LayoutParams.MATCH_PARENT, 15 WindowManager.LayoutParams.MATCH_PARENT, 16 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, //オーバーレイヤーに設定 17 WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 18 PixelFormat.TRANSLUCENT //ウインドウの透明化 19 ); 20 21 //WindowManagerを取得 22 mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 23 24 //レイアウトファイルから重ねあわせするViewを作成 25 mView = layoutInflater.inflate(R.layout.filter, null); 26 27 //透過率80の黒色のフィルターを指定 28 //パラメータは左から(透過率,R,G,B) ココをいじれば任意のフィルターに変更可能 29 mView.setBackgroundColor(Color.argb(50, 0, 0, 0)); 30 31 //Viewを画面上に重ねあわせする 32 mWindowManager.addView(mView, layoutParams); 33 34 return START_STICKY; 35 } 36 37 @Override 38 public void onDestroy() { 39 super.onDestroy(); 40 mWindowManager.removeView(mView); 41 } 42 43 @Nullable 44 @Override 45 public IBinder onBind(Intent intent) { 46 return null; 47 } 48 @Override 49 public void onRebind(Intent intent){} 50 51 @Override 52 public boolean onUnbind(Intent intent){ 53 return true; 54 } 55} 56 57
###補足情報(言語/FW/ツール等のバージョンなど)
Java Android Studioを使っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。