「Unity/Native連携/Kotlinバックグランドアラーム」の版間の差分
提供: 初心者エンジニアの簡易メモ
行1: | 行1: | ||
− | Unity側 | + | ==Unity側== |
RenkeiScene.cs | RenkeiScene.cs | ||
<pre> | <pre> | ||
行45: | 行45: | ||
} | } | ||
</pre> | </pre> | ||
− | Android側 | + | ==Android側== |
AlarmPermissionHelper.kt | AlarmPermissionHelper.kt | ||
<pre> | <pre> |
2024年9月10日 (火) 17:13時点における版
Unity側
RenkeiScene.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class RenkeiScene : MonoBehaviour { [SerializeField] Button button; void Start() { button.onClick.AddListener(CallMethod); } void CallMethod() { SetAlarm(0, 0, 10); } #if UNITY_ANDROID && !UNITY_EDITOR public void SetAlarm(int hour, int minute, int second) { Debug.Log("SetAlarm hour=" + hour + " min=" + minute + " second=" + second); using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext"); AndroidJavaClass alarmReceiver = new AndroidJavaClass("com.example.mylibrary.AlarmReceiver"); alarmReceiver.CallStatic("setAlarm", context, hour, minute, second); } } // 権限付与 public void RequestExactAlarmPermission() { using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject alarmPermissionHelper = new AndroidJavaObject("com.example.alarmapp.AlarmPermissionHelper"); alarmPermissionHelper.Call("requestExactAlarmPermission", activity); } } #endif }
Android側
AlarmPermissionHelper.kt
package com.example.mylibrary import android.content.Intent import android.provider.Settings import android.app.AlarmManager import android.content.Context import android.os.Build import android.net.Uri class AlarmPermissionHelper { fun requestExactAlarmPermission(context: Context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager if (!alarmManager.canScheduleExactAlarms()) { // アラームの許可がない場合、設定画面を開く val intent = Intent().apply { action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM data = Uri.parse("package:${context.packageName}") } context.startActivity(intent) } } } }
AlarmReceiver.kt
package com.example.mylibrary import android.app.AlarmManager import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.SystemClock import android.util.Log import android.widget.Toast import java.util.Calendar class AlarmReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val message = "時間です" Toast.makeText(context, message, Toast.LENGTH_SHORT).show() } companion object { @JvmStatic fun setAlarm(context: Context, hour: Int, minute: Int, second: Int) { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(context, AlarmReceiver::class.java) val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) val triggerAtMillis = SystemClock.elapsedRealtime() + calculateTriggerTime(hour, minute, second) alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent) } private fun calculateTriggerTime(hour: Int, minute: Int, second: Int): Long { // 現在の時間を取得 val now = System.currentTimeMillis() val calendar = Calendar.getInstance() calendar.timeInMillis = now // 現在の時間に指定された時間、分、秒を加算 calendar.add(Calendar.HOUR_OF_DAY, hour) calendar.add(Calendar.MINUTE, minute) calendar.add(Calendar.SECOND, second) // 計算された時間が現在の時間より後であることを確認 return calendar.timeInMillis - now } } }
Assets/Plugins/Android/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> <application> <receiver android:name="com.example.mylibrary.AlarmReceiver" android:enabled="true" android:exported="true" /> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> </manifest>