前提・実現したいこと
初めて質問させていただきます。
Androidでアプリやブラウザ上に仮想ボタンのようなものを表示させ続けるアプリを作ろうと考えております。
現在、アプリのトップ画面を形成するActivity内にServiceを開始する(仮想ボタンを表示させる)ボタンとSpinnerを設定しており、Spinnerで選択された値によって仮想ボタンが表示される初期位置を決められるようにしたいと考えております。
そこで、Activity内のSpinnerで選択された値をService側へ送ればよいのではないかと考えており、intentを使ってActivityからServiceへ値を送りたいと考えております。しかし、Service側で値が参照できないのか、うまく値を送ることができません。
ActivityからServiceへ値を送るにはどうすればよいのでしょうか?よくわかっていない部分も多く、コードも見づらいとは思いますがどなたかご助力いただければ幸いです。
Compilation failed; see the compiler error output for details.
該当のソースコード
Java
1【MainActivity.java】 2 3import android.annotation.TargetApi; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.os.Build; 7 import android.provider.Settings; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.util.Log; 11 import android.view.View; 12 import android.widget.AdapterView; 13 import android.widget.ArrayAdapter; 14 import android.widget.Button; 15 import android.widget.Spinner; 16 17public class MainActivity extends AppCompatActivity { 18 19 public static int OVERLAY_PERMISSION_REQ_CODE = 1000; 20 21 public String spinnerItems[] = {"①","②","③","④"}; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 Button backHomeButton = findViewById(R.id.Button1); 29 backHomeButton.setOnClickListener(new View.OnClickListener() { 30 public void onClick(View v) { 31 // ホーム画面に戻るインテントを起動 32 Intent homeIntent = new Intent(Intent.ACTION_MAIN); 33 homeIntent.addCategory(Intent.CATEGORY_HOME); 34 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 35 MainActivity.this.startActivity(homeIntent); 36 } 37 }); 38 39 Spinner spinner = findViewById(R.id.spinner); 40 41 // ArrayAdapter 42 ArrayAdapter<String> adapter 43 = new ArrayAdapter<>(this, 44 android.R.layout.simple_spinner_item, spinnerItems); 45 46 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 47 48 // Spinnerにadapterをセット 49 spinner.setAdapter(adapter); 50 51 // リスナーを登録 52 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 53 54 // アイテムが選択された時 55 @Override 56 public void onItemSelected(AdapterView<?> parent, 57 View view, int position, long id) { 58 Spinner spinner = (Spinner)parent; 59 final String item = (String)spinner.getSelectedItem(); 60 61 // Serviceを開始するためのボタン 62 final Button bt = findViewById(R.id.Button2); 63 bt.setOnClickListener(new View.OnClickListener() { 64 @Override 65 public void onClick(View v) { 66 67 if(item.equals("①")) { 68 Intent intent = new Intent(getApplication(), TestService.class); 69 // intentにデータを入れる 70 intent.putExtra("①", item); 71 // Serviceの開始 72 // API26以上 73 startForegroundService(intent); 74 bt.setEnabled(false); 75 }else if(item.equals("②")) { 76 Intent intent = new Intent(getApplication(), TestService.class); 77 intent.putExtra("②", item); 78 startForegroundService(intent); 79 bt.setEnabled(false); 80 }else if(item.equals("③")) { 81 Intent intent = new Intent(getApplication(), TestService.class); 82 intent.putExtra("③", item); 83 startForegroundService(intent); 84 bt.setEnabled(false); 85 }else{ 86 Intent intent = new Intent(getApplication(), TestService.class); 87 intent.putExtra("④", item); 88 startForegroundService(intent); 89 bt.setEnabled(false); 90 } 91 92 } 93 }); 94 } 95 96 // アイテムが選択されなかった 97 public void onNothingSelected(AdapterView<?> parent) { 98 99 } 100 }); 101 102 // API 23 以上であればPermission checkを行う 103 //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 104 checkPermission(); 105 //} 106 107 108 } 109 110 @TargetApi(Build.VERSION_CODES.M) 111 public void checkPermission() { 112 if (!Settings.canDrawOverlays(this)) { 113 Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 114 Uri.parse("package:" + getPackageName())); 115 startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE); 116 } 117 } 118 119 @TargetApi(Build.VERSION_CODES.M) 120 @Override 121 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 122 if (requestCode == OVERLAY_PERMISSION_REQ_CODE) { 123 if (!Settings.canDrawOverlays(this)) { 124 Log.d("debug","SYSTEM_ALERT_WINDOW permission not granted..."); 125 // SYSTEM_ALERT_WINDOW permission not granted... 126 // nothing to do ! 127 } 128 } 129 } 130}
Java
1【TestService.java】 2 3import android.app.Notification; 4import android.app.NotificationChannel; 5import android.app.NotificationManager; 6import android.app.PendingIntent; 7import android.app.Service; 8import android.content.Context; 9import android.content.Intent; 10import android.graphics.PixelFormat; 11import android.net.Uri; 12import android.os.IBinder; 13import android.util.Log; 14import android.view.Gravity; 15import android.view.LayoutInflater; 16import android.view.MotionEvent; 17import android.view.View; 18import android.view.ViewGroup; 19import android.view.WindowManager; 20import android.widget.ImageButton; 21import android.widget.TableLayout; 22 23public class TestService extends Service { 24 25 //画面に全面表示させるビュー 26 private View view; 27 private WindowManager windowManager; 28 private int dpScale; 29 private ImageButton button1, button2, button3, button4, button5, button6; 30 private TableLayout tableLayout; 31 32 @Override 33 public void onCreate() { 34 super.onCreate(); 35 36 // dipを取得 37 dpScale = (int)getResources().getDisplayMetrics().density; 38 39 } 40 41 @Override 42 public int onStartCommand(Intent intent, int flags, int startId) { // startServiceでサービスが開始要求を受けた時のコールバック。 43 // startForegroundService() ----- 44 45 Context context = getApplicationContext(); 46 String channelId = "default"; 47 String title = context.getString(R.string.app_name); 48 49 ~(略)~ 50 Intent intent = getIntent(); 51 String item = intent.getStringExtra("①,②,③,④"); 52 params.gravity = Gravity.TOP | Gravity.END; 53 if(item = intent.getStringExtra("①")) { 54 // 左上(①)に配置 55 params.x = 200 * dpScale; // 200dp 56 params.y = 80 * dpScale; // 80dp 57 }else if(item = intent.getStringExtra("②")) { 58 // 右上(②)に配置 59 params.x = 20 * dpScale; // 20dp 60 params.y = 80 * dpScale; // 80dp 61 }else if(item = intent.getStringExtra("③")) { 62 // 左下(③)に配置 63 params.x = 200 * dpScale; // 200dp 64 params.y = 300 * dpScale; // 65 }else { 66 // 右下(④)に配置 67 params.x = 20 * dpScale; // 20dp 68 params.y = 300 * dpScale; // 300dp 69 } 70~(略)~
試したこと
Intent intent = getIntent(); ~(略)~ // 右下(④)に配置 params.x = 20 * dpScale; // 20dp params.y = 300 * dpScale; // 300dp }
の部分にエラーが出ております。if文(あるいはそれ以外の条件文)を使うこと自体がおかしいのか、ServiceではgetIntentを用いること自体ができないのか、様々なサイトを調べましたが、解決方法が見つかりませんでした。
補足情報(FW/ツールのバージョンなど)
言語:Java
開発環境:Android Studio3.1.3
回答1件
あなたの回答
tips
プレビュー