Android服务无法使用

时间:2017-06-23 12:16:29

标签: c# android xamarin.android android-service

我无法启动我的Android服务。我想在我的片段中调用它,但它不起作用,并且没有显示任何错误。

这是我的片段类。我正在尝试使用此代码启动服务:

Activity.StartService(new Intent(Activity, typeof(SimpleStartedService)));

My Fragment的代码:

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})." );
    }
}

1 个答案:

答案 0 :(得分:0)

清单文件中的声明必须正确:

<service android:enabled="true" android:name="<PACKAGE_NAME_HERE.SERVICE_CLASS_NAME_HERE>" /> 

另外,检查名称中的大写和小写字母,两个地方必须相同。