コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted,
"android.intent.action.QUICKBOOT_POWERON",
"com.htc.intent.action.QUICKBOOT_POWERON",
"android.intent.action.PACKAGE_INSTALL",
"android.intent.action.PACKAGE_ADDED",
Intent.ActionMyPackageReplaced })]
public class BootReceiver : BroadcastReceiver
{
private static PowerManager.WakeLock _wakeLock = null;
public override void OnReceive(Context context, Intent intent)
{
//画面ロックの取得
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
_wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "BootReceiver");
_wakeLock.Acquire();
//リマインダーをセット
this.SetReminder(context);
//サービスの起動
//以前の記事でご紹介済みなので割愛
}
//Android用のリマインダー
public void SetReminder(Context context)
{
Intent alarmIntent = new Intent(context, typeof(BootReceiver));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
//AlarmManagerで1時間に1回の実行を予約する
long fireTime = SystemClock.ElapsedRealtime() + 60 * 1000 * 60; //60s*1000ms*60minutes=1hour
alarmManager.SetInexactRepeating(AlarmType.ElapsedRealtimeWakeup, fireTime, AlarmManager.IntervalHour, pendingIntent);
}
public static void ReleaseWakeLock()
{
if (_wakeLock != null)
{
_wakeLock.Release();
}
}
}
項目 | 説明 |
---|---|
RtcWakeup (値0) | UTC時刻を指定する (スリープ状態のときは電源をONにしてくれる) |
Rtc (値1) | UTC時刻を指定する |
ElapsedRealtimeWakeup (値2) | 端末が起動してからの経過時間で指定する (スリープ状態のときは電源をONにしてくれる) |
ElapsedRealtime (値3) | 端末が起動してからの経過時間で指定する |
[Service(Name = "com.CompanyName.BootReceiverTest.BackgroundService", Exported = false, Process = ":TestProcess")]
public class BackgroundService : IntentService
{
public override IBinder OnBind(Intent intent)
{
return null;
}
//ServicのOnStartCommandはコメントアウトする(バックグラウンドサービスが再起動しないように)
//public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
//{
// Thread t = new Thread(() =>
// {
// //Xamarinを有効化
// var bundle = new Bundle();
// global::Xamarin.Forms.Forms.Init(this, bundle);
// while (true)
// {
// System.Threading.Thread.Sleep(10000);
// NotificationService nService = new NotificationService(this);
// nService.On("BackgroundService", "BackgroundService.OnStartCommand");
// }
// });
// t.Start();
// return StartCommandResult.Sticky;
//}
//OnHandleIntentを追加する
private bool _endThread = false;
protected override void OnHandleIntent(Intent intent)
{
//Xamarinを有効化
var bundle = new Bundle();
global::Xamarin.Forms.Forms.Init(this, bundle);
_endThread = false;
while (true)
{
System.Threading.Thread.Sleep(10000);
NotificationService nService = new NotificationService(this);
nService.On("BackgroundService", "BackgroundService.OnStartCommand");
break;
}
BootReceiver.ReleaseWakeLock();
_endThread = true;
}
public void StartBackgroundService()
{
System.Diagnostics.Debug.WriteLine("●StartBackgroundService");
//サービスを起動する
base.StartService(this.GetServiceIntent());
}
public void StopBackgroundService()
{
System.Diagnostics.Debug.WriteLine("●StopBackgroundService");
//サービスを停止する
base.StopService(this.GetServiceIntent());
}
private Intent GetServiceIntent()
{
System.Diagnostics.Debug.WriteLine("●BackgroundService.GetServiceIntent");
//サービスを起動する
Intent serviceIntent = new Intent(this, typeof(BackgroundService));
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop &&
Build.VERSION.SdkInt <= BuildVersionCodes.LollipopMr1)
{
//Android5 Lollipop対応
string packageName = this.PackageManager.GetPackageInfo(this.PackageName, 0).PackageName;
serviceIntent.SetPackage(packageName);
serviceIntent.SetClassName(this, packageName + ".BackgroundService");
}
else
{
serviceIntent.AddFlags(ActivityFlags.NewTask);
}
return serviceIntent;
}
public override void OnDestroy()
{
base.OnDestroy();
//処理が中断された場合
if (!_endThread)
{
//Killされてもサービスを再起動する。
this.StartBackgroundService();
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。