전에 froyo가 설정 되었기 때문에 쉽게 사용할 수 있지만 악용하기 쉽습니다.
이제는 알림을 활성화해야하는 startForeGround 서비스가 있습니다 (사용자가 전경 서비스가 실행중인 것을 볼 수 있도록).
내가 그것을 제어하는이 클래스를 만든 :
내 미디어 플레이어이 업데이트 알림에 대한 다음
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);
}
}
- 당신은
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;
}
};
을 당신이 어떤 서비스에 바인딩하는 것처럼 당신은 단지 bindService 통화를 (이 문제는 전경 날씨 여부 나던) ONSTART 활동의 일반적인 방법으로 결합 요청 작업 내가 할 수있는 유일한 일을 수행하기 위해
"onewcreate"란 무엇인가요? 필기체 오류입니까 아니면 약간의 지식입니까? – pengwang
oppps 미안하지만 onCreate - 예외보고를위한 내 자신의 래퍼가 표시됩니다. – siliconeagle
나는 API 데모 데모와 같은 이상한 행동을 발견했다. API 데모 (API 버전 7)로 이동하여 서비스 시작 - 서비스 -> 서비스 -> 로컬 서비스 컨트롤러로 이동하십시오. 이제 notif의 알림. 바가 가동 중입니다. 이제 홈 버튼과 notif를 눌러주십시오. 서비스 알림 (샘플 로컬 서비스)을 누릅니다. 지금!실제로 돌아가려면 뒤로 버튼을 세 번 눌러야합니다. 왜 세계에서? !!? – zmeda