facebook twitter hatena line email

「Unity/Native連携/Kotlinバックグランドアラーム」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Admin がページ「Unity/Native連携/Kotlinバックグランド通知」を「Unity/Native連携/Kotlinバックグランドアラーム」に、リダイレクトを残さずに移動しました)
 
(相違点なし)

2024年9月11日 (水) 17:16時点における最新版

Unity側

RenkeiScene.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class RenkeiScene : MonoBehaviour
{
    [SerializeField] Button alarmButton;
    void Start()
    {
        alarmButton.onClick.AddListener(CallMethodAlarm);
    }
    void CallMethodAlarm()
    {
        SetAlarm(10);
    }
#if UNITY_ANDROID && !UNITY_EDITOR
    public void SetAlarm(int second)
    {
        Debug.Log("SetAlarm 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, 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, 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(second)
            alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent)
        }

        private fun calculateTriggerTime(second: Int): Long {
            // 現在の時間を取得
            val now = System.currentTimeMillis()
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = now

            // 現在の時間に指定された秒を加算
            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>