Techbox Android 05 Service
Techbox Android 05 Service
Service
Projeto TechBox | Service
Cdigo do Service
public class LocalService extends Service {
private NotificationManager mNM;
@Override
public void onCreate() {
mNM =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int
startId) {
Log.i("LocalService", "Received start id " +
startId + ": " + intent);
return START_STICKY;
}
@Override
public void onDestroy() {
mNM.cancel(NOTIFICATION);
Toast.makeText(this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
Cdigo do Cliente
private LocalService mBoundService;
mBoundService =
((LocalService.LocalBinder)service).getService();
Toast.makeText(Binding.this,
R.string.local_service_connected,
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;
Projeto TechBox | Service
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
Messenger Service
Um exemplo de Service que usa um Messenger como cliente.
public class MessengerService extends Service {
NotificationManager mNM;
ArrayList<Messenger> mClients = new
ArrayList<Messenger>();
int mValue = 0;
case MSG_REGISTER_CLIENT:
mClients.add(msg.replyTo);
break;
case MSG_UNREGISTER_CLIENT:
mClients.remove(msg.replyTo);
break;
case MSG_SET_VALUE:
mValue = msg.arg1;
Projeto TechBox | Service
mClients.get(i).send(Message.obtain(null, MSG_SET_VALUE,
mValue, 0));
} catch (RemoteException e) {
mClients.remove(i);
}
}
break;
default:
super.handleMessage(msg);
}
}
}
@Override
public void onCreate() {
mNM =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public void onDestroy() {
mNM.cancel(R.string.remote_service_started);
Toast.makeText(this,
R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this,
Controller.class), 0);
notification.setLatestEventInfo(this,
getText(R.string.remote_service_label), text,
contentIntent);
mNM.notify(R.string.remote_service_started,
notification);
}
}
<service android:name=".app.MessengerService"
android:process=": remote" />
case MessengerService.MSG_SET_VALUE:
mCallbackText.setText("Received from
service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
}
try {
Message msg = Message.obtain(null,
MessengerService.MSG_REGISTER_CLIENT);
msg.replyTo = mMessenger;
mService.send(msg);
msg = Message.obtain(null,
MessengerService.MSG_SET_VALUE, this.hashCode(), 0);
mService.send(msg);
} catch (RemoteException e) {
}
Toast.makeText(Binding.this,
R.string.remote_service_connected,
Toast.LENGTH_SHORT).show();
}
Toast.makeText(Binding.this,
R.string.remote_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
void doBindService() {
bindService(new Intent(Binding.this,
MessengerService.class), mConnection,
Context.BIND_AUTO_CREATE);
mIsBound = true;
mCallbackText.setText("Binding.");
}
void doUnbindService() {
if (mIsBound) {
if (mService != null) {
try {
Projeto TechBox | Service
unbindService(mConnection);
mIsBound = false;
mCallbackText.setText("Unbinding.");
}
}