私のAndroidサービスを開始できません。私はそれを私の断片で呼んでいますが、機能していないし、何の誤りも見せていません。Androidサービスが動作していません
私の断片クラスです。私はこのコードでサービスを開始しようとしています:
Activity.StartService(new Intent(Activity, typeof(SimpleStartedService)));
マイ・フラグメントのコード:
class SettingsFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static Settings NewInstance()
{
var settings = new Settings { Arguments = new Bundle() };
return settings;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.settings, null);
var btnStartService = view.FindViewById<Button>(Resource.Id.btnStartService);
btnStartService.Click += delegate
{
Activity.StartService(new Intent(Activity, typeof(SimpleStartedService)));
};
return view;
}
}
ここでは私のサービスです。プログラムはここに入っていません。
[Service]
public class SimpleStartedService : Service
{
static readonly string TAG = "X:" + typeof(SimpleStartedService).Name;
static readonly int TimerWait = 4000;
Timer timer;
DateTime startTime;
bool isStarted = false;
public override void OnCreate()
{
base.OnCreate();
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug(TAG, $"OnStartCommand called at {startTime}, flags={flags}, startid={startId}");
if (isStarted)
{
TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
Log.Debug(TAG, $"This service was already started, it's been running for {runtime:c}.");
}
else
{
startTime = DateTime.UtcNow;
Log.Debug(TAG, $"Starting the service, at {startTime}.");
timer = new Timer(HandleTimerCallback, startTime, 0, TimerWait);
isStarted = true;
}
return StartCommandResult.NotSticky;
}
public override IBinder OnBind(Intent intent)
{
// This is a started service, not a bound service, so we just return null.
return null;
}
public override void OnDestroy()
{
timer.Dispose();
timer = null;
isStarted = false;
TimeSpan runtime = DateTime.UtcNow.Subtract(startTime);
Log.Debug(TAG, $"Simple Service destroyed at {DateTime.UtcNow} after running for {runtime:c}.");
base.OnDestroy();
}
void HandleTimerCallback(object state)
{
TimeSpan runTime = DateTime.UtcNow.Subtract(startTime);
Log.Debug(TAG, $"This service has been running for {runTime:c} (since ${state}).");
}
}
マニフェストにエントリを追加しましたか? – Sufian
はい、それでも動作しません – Likya07
あなたのマニフェストも投稿してください。 – Sufian