IDが生成されたときに、サービスの起動を行うようにした #32

Merged
asura146 merged 1 commits from hotfix-notification into main 2024-08-04 16:13:04 +00:00
4 changed files with 15 additions and 7 deletions

View File

@ -35,7 +35,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TestService" />
<service android:name=".SurveillanceService" />
</application>
</manifest>

View File

@ -14,7 +14,6 @@ import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
@ -134,7 +133,7 @@ public class MainActivity extends AppCompatActivity {
}
registerReceiver(receiver, intentFilter);
startForegroundService(new Intent(this, TestService.class));
startForegroundService(new Intent(this, SurveillanceService.class));
}

View File

@ -3,6 +3,8 @@ package com.example.childguard;
import static android.content.ContentValues.TAG;
import static android.content.Context.MODE_PRIVATE;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
@ -95,6 +97,7 @@ public class SettingFragment extends Fragment {
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putString("ID", documentReference.getId());
editor.apply();
startTestService();
Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show();
getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(documentReference.getId())).commit();
@ -106,4 +109,11 @@ public class SettingFragment extends Fragment {
}
});
}
private void startTestService() {
Context context = getContext();
if (context != null) {
Intent serviceIntent = new Intent(context, SurveillanceService.class);
context.startForegroundService(serviceIntent);
}
}
}

View File

@ -27,9 +27,7 @@ import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import org.checkerframework.checker.index.qual.LengthOf;
public class TestService extends Service {
public class SurveillanceService extends Service {
private final Handler handler = new Handler();
private Runnable notificationRunnable;
@ -81,6 +79,7 @@ public class TestService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
this.userId = getSharedPreferences("app_situation", MODE_PRIVATE).getString("ID", null);
if (this.userId == null) {
Log.d("onResume", "ID not initialized.");
@ -256,7 +255,7 @@ public class TestService extends Service {
private PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setAction("OPEN_ACTIVITY");
return PendingIntent.getActivity(context, TestService.REQUEST_CODE, intent, PendingIntent.FLAG_IMMUTABLE);
return PendingIntent.getActivity(context, SurveillanceService.REQUEST_CODE, intent, PendingIntent.FLAG_IMMUTABLE);
}
/**