2017-08-11 105 views
0

我试着去创造xamarin媒体播放器服务,但每当我打电话Startservice这样无法创建服务Xamarin

StartService(new Intent(ApplicationContext,typeof(StreamingBackgroundService))); 

的onCreate方法永远不会被调用。我已经加入该服务的AndroidManifest.xml中 这样

<application android:label="PlayYoutubeMP3" android:icon="@drawable/Icon"></application> 

但它不无论做任何事情。我正在使用Android API 23和Visual Studio 2017.我真的不知道我做错了什么。任何帮助将不胜感激

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Media; 

    [Service(Label = "StreamingBackgroundService", Icon = "@drawable/Icon")] 
    public class StreamingBackgroundService : Service, SendCommand 
    { 
     private MediaPlayer player; 


     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 


     public override void OnCreate() 
     { 
      player = new MediaPlayer(); 
      MainActivity.Event = this; 
      base.OnCreate(); 
     } 
     public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
     { 
      //Set sticky as we are a long running operation 
      return StartCommandResult.Sticky; 
     } 

     public void Play() 
     { 
      player.Start(); 
     } 

     private void Pause() 
     { 
      player.Pause(); 
     } 

     private void Stop() 
     { 
      player.Stop(); 
     } 

     public void SendCommand(Command C, object Param = null) 
     { 
      switch (C) 
      { 
       case Command.Start: 
        Play(); 
        break; 
       case Command.Stop: 
        Stop(); 
        break; 
       case Command.Pause: 
        Pause(); 
        break; 
       case Command.Reset: 
        player.Reset(); 
        break; 
       case Command.GoBack: 

        break; 
       case Command.GoForward: 

        break; 
       case Command.Initialize: 
        player.SetDataSource((string)Param); 
        player.Prepare(); 
        player.SetWakeMode(ApplicationContext, WakeLockFlags.Partial); 
        break; 
       case Command.ShouldLoop: 
        player.Looping = (bool)Param; 
        break; 
       default: 
        break; 
      } 
     } 
    } 
} 
+0

'没什么反应'...你期望发生什么?在OnCreate()方法中放置一个断点以确认您的StartService调用实际上正在创建该服务。同样使用静态的Activity引用来调用服务中的方法通常是一个坏主意,您应该使用绑定的服务来访问服务方法 – SushiHangover

+0

我从来没有调用过 –

+1

'[Service]'属性句柄在构建过程中更新清单,因此您不应手动更新清单,因为默认情况下它将使用md5名称。 – SushiHangover

回答

0

我把它改成了一个绑定的服务,如SushiHangover建议。这工作