Cześć,
mam problem z serwisem. Gdy uruchamiam aplikację i nacisnę przycisk bind, na pasku powiadomień pojawia się ikona i tekst, serwis jest uruchamiany i bindowany z aktywnością, następnie klikam na przycisk unbind i serwis jest zatrzymywany.
ale gdy uruchomię aplikacje, nacisnę na przycisk bind(serwis zostaje uruchomiony i zbindowany), następnie kliknę przycisk home, rozwinę pasek powiadomień i kliknę w ikona aby włączyć aplikacje, mogę do woli wciskać przycisk unbind i nic się nie dzieje;/ czy moglibyście pomoc mi z tym problemem?
poniżej zamieszczam kod aktywności i serwisu, prawie cały kod jest z dokumentacji androida.
public class Binding extends Activity {
private boolean mIsBound;
private LocalService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((LocalService.LocalBinder) service).getService();
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
void doBindService() {
bindService(new Intent(Binding.this, LocalService.class), mConnection,
Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
private OnClickListener mBindListener = new OnClickListener() {
public void onClick(View v) {
doBindService();
}
};
private OnClickListener mUnbindListener = new OnClickListener() {
public void onClick(View v) {
doUnbindService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.local_service_binding);
Button button = (Button) findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button) findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
}
@Override
protected void onResume() {
super.onResume();
if (LocalService.isRunning()) {
doBindService();
}
}
}
kod serwisu
public class LocalService extends Service {
private NotificationManager mNM;
private static boolean isRunning = false;
private int NOTIFICATION = R.string.local_service_started;
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
isRunning = true;
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
isRunning = true;
return START_STICKY;
}
public static boolean isRunning() {
return isRunning;
}
@Override
public void onDestroy() {
mNM.cancel(NOTIFICATION);
isRunning = false;
Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
private void showNotification() {
CharSequence text = getText(R.string.local_service_started);
Notification notification = new Notification(R.drawable.icon_small, text,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Binding.class), 0);
notification.setLatestEventInfo(this, getText(R.string.local_service_label),
text, contentIntent);
mNM.notify(NOTIFICATION, notification);
}
}