コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
public static bool IsServiceRunning(Java.Lang.Class nService)
{
ActivityManager manager = (ActivityManager)Android.App.Application.Context.GetSystemService(Context.ActivityService);
foreach (ActivityManager.RunningServiceInfo service in manager.GetRunningServices(int.MaxValue))
{
System.Diagnostics.Debug.WriteLine("service.Service.ClassName:" + service.Service.ClassName);
if (nService.Name.Equals(service.Service.ClassName))
{
System.Diagnostics.Debug.WriteLine("IsServiceRunning=true");
return true;
}
}
System.Diagnostics.Debug.WriteLine("IsServiceRunning=false");
return false;
}
private bool IsTaskRunning(Java.Lang.Class nService)
{
ActivityManager manager = (ActivityManager)Android.App.Application.Context.GetSystemService(Context.ActivityService);
foreach (ActivityManager.RunningTaskInfo task in manager.GetRunningTasks(int.MaxValue))
{
System.Diagnostics.Debug.WriteLine("task.BaseActivity.ClassName:" + task.BaseActivity.ClassName);
if (nService.Name.Equals(task.BaseActivity.ClassName))
{
System.Diagnostics.Debug.WriteLine("IsTaskRunning=true");
return true;
}
}
System.Diagnostics.Debug.WriteLine("IsTaskRunning=false");
return false;
}
//サービスが起動しているか確認する
BackgroundService bService = new BackgroundService();
if (!IsServiceRunning(bService.Class))
{
//サービスを起動
Intent serviceIntent = new Intent(bService, typeof(BackgroundService));
serviceIntent.AddFlags(ActivityFlags.NewTask);
context.StartService(serviceIntent);
}
//アプリが起動しているか確認する
MainActivity mActivity = new MainActivity();
if (!IsTaskRunning(mActivity.Class))
{
//アプリを起動
Intent activityIntent = new Intent(context, typeof(MainActivity));
activityIntent.SetFlags(ActivityFlags.SingleTop); //複数のActivityを起動しない
activityIntent.AddFlags(ActivityFlags.NewTask);
context.StartActivity(activityIntent);
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。