diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 98cbd3d..9919eb8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,6 +80,8 @@ + + diff --git a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java index 5240bad..4ce93a6 100644 --- a/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java +++ b/app/src/main/java/com/example/cosmosclient/services/CosomosBackgroundService.java @@ -1,15 +1,30 @@ package com.example.cosmosclient.services; +import android.Manifest; import android.app.IntentService; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Intent; import android.content.Context; +import android.content.pm.PackageManager; +import android.graphics.Color; import android.location.Location; import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.RequiresApi; +import android.support.v4.app.ActivityCompat; import android.util.Log; +import com.example.cosmosclient.R; + public class CosomosBackgroundService extends IntentService implements LocationListener { + private Context context; + private LocationManager locationManager; public CosomosBackgroundService() { super("CosomosBackgroundService"); @@ -19,8 +34,12 @@ public void onCreate() { super.onCreate(); Log.d("debug", "onCreate"); + context = getApplicationContext(); + // LocationManager インスタンス生成 + locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } + @RequiresApi(api = Build.VERSION_CODES.O) @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -43,7 +62,44 @@ // } // }; // thread.start(); + int requestCode = 0; + String channelId = "default"; + String title = context.getString(R.string.app_name); + PendingIntent pendingIntent = + PendingIntent.getActivity(context, requestCode, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + // ForegroundにするためNotificationが必要、Contextを設定 + NotificationManager notificationManager = + (NotificationManager)context. + getSystemService(Context.NOTIFICATION_SERVICE); + // Notification Channel 設定 + NotificationChannel channel = new NotificationChannel( + channelId, title , NotificationManager.IMPORTANCE_DEFAULT); + channel.setDescription("Silent Notification"); + // 通知音を消さないと毎回通知音が出てしまう + // この辺りの設定はcleanにしてから変更 + channel.setSound(null,null); + // 通知ランプを消す + channel.enableLights(false); + channel.setLightColor(Color.BLUE); + // 通知バイブレーション無し + channel.enableVibration(false); + if(notificationManager != null) { + notificationManager.createNotificationChannel(channel); + Notification notification = new Notification.Builder(context, channelId) + .setContentTitle(title) + // 本来なら衛星のアイコンですがandroid標準アイコンを設定 + .setSmallIcon(android.R.drawable.btn_star) + .setContentText("GPS") + .setAutoCancel(true) + .setContentIntent(pendingIntent) + .setWhen(System.currentTimeMillis()) + .build(); + // startForeground + startForeground(1, notification); + } +// startGPS(); return super.onStartCommand(intent,flags,startId); } @@ -64,9 +120,35 @@ } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - } - +// protected void startGPS() { +// StringBuilder strBuf = new StringBuilder(); +// strBuf.append("startGPS\n"); +// +// final boolean gpsEnabled +// = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); +// if (!gpsEnabled) { +// // GPSを設定するように促す +// enableLocationSettings(); +// } +// +// if (locationManager != null) { +// try { +// if (ActivityCompat.checkSelfPermission(this, +// Manifest.permission.ACCESS_FINE_LOCATION)!= +// PackageManager.PERMISSION_GRANTED) { +// return; +// } +// +// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, +// MinTime, MinDistance, this); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } else { +// strBuf.append("locationManager=null\n"); +// } +// } @Override public void onDestroy() { Log.d("debug", "onDestroy"); diff --git a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java index 29ef5a5..aca8b2d 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SigninActivity.java @@ -35,7 +35,6 @@ private Intent intentservice; - @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -58,14 +57,15 @@ // } // }); -// Thread thread = new Thread() { -// @RequiresApi(api = Build.VERSION_CODES.O) -// public void run() { + Thread thread = new Thread() { + @RequiresApi(api = Build.VERSION_CODES.O) + public void run() { intentservice = new Intent(SigninActivity.this, CosomosBackgroundService.class); startForegroundService(intentservice); -// } -// }; -// thread.start(); + + } + }; + thread.start(); //各種IDを取得 SigninButton = findViewById(R.id.SigninButton); Button SignupButton = findViewById(R.id.SignupButton);