onStopを追加

This commit is contained in:
meato 2024-01-17 17:43:54 +09:00
parent 38b509f5d2
commit a4178b6901
4 changed files with 87 additions and 1 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -34,6 +35,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TestService" />
</application>
</manifest>

View File

@ -241,6 +241,15 @@ public class MainActivity extends AppCompatActivity {
notificationManager.notify(R.string.app_name, builder.build());
}
@Override
public void onStop() {
super.onStop();
Intent intent = new Intent(getApplication(), TestService.class);
startService(intent);
}
//Bluetooth_setupの戻るボタン

View File

@ -0,0 +1,74 @@
package com.example.childguard;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class TestService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
audioStart();
return START_NOT_STICKY;
}
private void audioStart(){
//通知をする際に起動するバイブレーション
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
//通知のやつ
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "通報通知", importance);
//説明説明 ここに通知の説明を書くことができる
channel.setDescription("第3者からの通報を検知しました");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
//通知のやつ
Log.d("nt","レスポンスを検知しました2");
//通知の詳細設定的な奴
NotificationCompat.Builder builder = new NotificationCompat
.Builder(this, "CHANNEL_ID")
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setContentTitle("通報検知")
.setContentText("子供の置き去りを検知しました。")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
notificationManager.notify(R.string.app_name, builder.build());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}