2011-03-21 57 views
5

做前台服务的正确方法是什么,我可以稍后绑定它? 我遵循Android API演示,其中包括如何创建前台服务的示例。 没有关于启动服务在同一时间绑定到它的例子。可绑定的Android前台服务

我想看到活动的音乐播放服务“绑定”到它的一个很好的例子。

有什么?

我想要做的事,如:

  • 当一个程序是第一(第一意味着服务尚未启动)开始我要开始前台服务,做所有的工作。用户界面(活动)就是当用户按下home键的服务必须保持正常运行(在栏通知必须存在),以控制工作
  • 现在,如果在通知栏活动的通知用户点击应该开始和绑定到服务(或类似的东西,正确的方式)和用户控制工作。
  • 如果活动是活跃和用户按下后退按钮活动应被销毁,也服务应该被销毁。

什么方法我必须重写来完成这项任务? Android的做法是什么?

回答

-3

为了完成任务,问我要做的唯一事情是追随性的AndroidManifest.xml添加到我的活动定义

android:launchMode="singleTop" 

就是这样。

问候

10

升级Froyo之前有setForeground(真)的服务,这是很容易,但也容易被滥用。

需要通知现在有startForeGround服务被激活(使用户可以看到有一个foregroundservice运行)。

我做了这个类来控制它:

public class NotificationUpdater { 
    public static void turnOnForeground(Service srv,int notifID,NotificationManager mNotificationManager,Notification notif) { 
     try { 
      Method m = Service.class.getMethod("startForeground", new Class[] {int.class, Notification.class}); 
      m.invoke(srv, notifID, notif); 
     } catch (Exception e) { 
      srv.setForeground(true); 
      mNotificationManager.notify(notifID, notif); 
     } 
    } 

    public static void turnOffForeground(Service srv,int notifID,NotificationManager mNotificationManager) { 
     try { 
      Method m = Service.class.getMethod("stopForeground", new Class[] {boolean.class}); 
      m.invoke(srv, true); 
     } catch (Exception e) { 
      srv.setForeground(false); 
      mNotificationManager.cancel(notifID); 
     } 
    } 
} 

然后我的媒体播放器,此更新的通知 - 注意,前台服务,而媒体播放和停止后,应留在只需要时,一个不好的做法。

private void updateNotification(){ 
    boolean playing = ((mFGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING) || 
         (mBGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING)); 
    if (playing) { 
     Notification notification = getNotification(); 
     NotificationUpdater.turnOnForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager,notification); 
    } else { 
     NotificationUpdater.turnOffForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager); 
    } 
} 

为绑定 - 你只是绑定在你的活动以正常的方式调用onStart你只要把bindService电话,你会绑定到任何服务(这并不重要天气它的前景还是不)

MediaPlayerService mpService=null; 
@Override 
protected void onEWCreate(Bundle savedInstanceState) { 
    Intent intent = new Intent(this, MediaPlayerService.class); 
    startService(intent); 
} 

@Override 
protected void onStart() { 
    // assume startService has been called already 
    if (mpService==null) { 
     Intent intentBind = new Intent(this, MediaPlayerService.class); 
     bindService(intentBind, mConnection, 0); 
    } 
} 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mpService = ((MediaPlayerService.MediaBinder)service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mpService = null; 
    } 
}; 
+0

什么是“onEWCreate”,是你的手写错误或我的小知识? – pengwang 2011-03-23 05:41:00

+0

oppps对不起,它显示onCreate - 这是我自己的异常报告封装。 – siliconeagle 2011-03-23 21:12:56

+0

我发现了API Demos演示的奇怪行为。如果您转到API演示(API版本7)并转到应用程序 - >服务 - >本地服务控制器,点击启动服务。现在通知在notif。酒吧正在运行。现在按HOME按钮,并通知。栏并按下服务通知(Sample Local Service)。现在!你必须按回三次才能返回。为什么在世界上?!!? – zmeda 2011-04-04 19:49:32