コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
namespace AppName.Services
{
public interface IDeviceService
{
//スリープを無効にする
void DisableSleep();
//スリープを有効にする
void EnableSleep();
}
}
<uses-permission android:name="android.permission.WAKE_LOCK" />
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Xamarin.Forms;
using AppName.Droid.Services;
using AppName.Services;
[assembly: Dependency(typeof(DeviceService))]
namespace AppName.Droid.Services
{
public class DeviceService : IDeviceService
{
private static PowerManager.WakeLock _wakeLock = null;
/// <summary>
/// スリープを無効にする
/// </summary>
public void DisableSleep()
{
PowerManager pm = (PowerManager)Forms.Context.GetSystemService(Context.PowerService);
Context context = Forms.Context; //Android.App.Application.Context;
var packageName = context.PackageManager.GetPackageInfo(context.PackageName, 0).PackageName;
_wakeLock = pm.NewWakeLock(WakeLockFlags.Full, packageName);
_wakeLock.Acquire();
}
/// <summary>
/// スリープを有効にする
/// </summary>
public void EnableSleep()
{
if (_wakeLock != null)
{
_wakeLock.Release();
_wakeLock = null;
}
}
}
}
using System;
using UIKit;
using Xamarin.Forms;
using AppName.iOS.Services;
using AppName.Services;
[assembly: Dependency(typeof(DeviceService))]
namespace AppName.iOS.Services
{
public class DeviceService : IDeviceService
{
/// <summary>
/// スリープを無効にする
/// </summary>
public void DisableSleep()
{
UIApplication.SharedApplication.IdleTimerDisabled = true;
}
/// <summary>
/// スリープを有効にする
/// </summary>
public void EnableSleep()
{
UIApplication.SharedApplication.IdleTimerDisabled = false;
}
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
void btnDisableSleep_Click(object sender, EventArgs e)
{
//スリープモードをOFFにする
DependencyService.Get<IDeviceService>().DisableSleep();
}
void btnEnableSleep_Click(object sender, EventArgs e)
{
//スリープモードをONにする
DependencyService.Get<IDeviceService>().EnableSleep();
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。