4 Nisan 2018 Çarşamba

Service Sınıfı

Giriş
İki çeşit servis var. İlki bound service. Bu servis tipi tüm bağlanan istemciler sonlanınca işletim sistemi tarafından yok edilir. İkincisi ise istemciye bağlı olmayan klasik servis tipi.

Servis Başlatmak ve Durdurma
Kendi servisimi başlatmak için şöyle yaparız.
Context context = ...
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
Durdurmak için şöyle yaparız.
context.stopService(new Intent(mContext,MyService.class));
Bir başka servisi başlatmak için şöyle yaparız
Intent intent = new Intent();
String pkg = currentService.service.getPackageName();
String cls = pkg + "." + currentService.service.getClassName();
intent.setComponent(new ComponentName(pkg, cls));
context.startService(intent);
IntentFilter ile Servis Başlatmak ve Durdurma
Service sınıfını içine şöyle bir değişken tanımlarız.
public static final String ServiceIntent = "custom.MY_SERVICE"
Servisi şöyle tanımlarız.
<service android:name=".services.MyService" android:enabled="true">
  <intent-filter android:label="@string/myService" >
    <action   android:name="custom.MY_SERVICE"/>
  </intent-filter>
</service>
Daha sonra servisi şöyle başlatıp durdurabiliriz.
startService(new Intent(MyService.ServiceIntent));
stopService(new Intent((MyService.ServiceIntent));
Manifest XML
name tag için şöyle yaparız.
<service
  android:name=".MyService"
  android:enabled="true"
  android:exported="true" />
process tag için şöyle yaparız. Böylece service başka bir process içinde çalışır.
<service
  android:name=".MyService"
  android:enabled="true"
  android:process=":my_process" />
stopWithTask tag için şöyle yaparız.
<service
  android:name=".ServiceTest"
  android:stopWithTask="true" />
Şöyle yaparız.
<service android:name=".MyService"
        android:label="MyService"
        android:stopWithTask="false"
        android:enabled="true"
        android:exported="true"
        />
Servis iskeleti
Şuna benzer
public class MyService extends Service {

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
  }
}
OnBind metodu
Şöyle yaparız.
@Nullable
@Override
public IBinder onBind(Intent intent) {
  return null;
}
OnCreate metodu
Servis içinde yaratılması gereken kaynak yaratılır. Şöyle yaparız.
public class MyService extends Service {

  Handler mHandler;

  @Override
  public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
  }
  ...
}
OnStart metodu
API 5'ten itibaren OnStartCommand kullanılmalı. Şöyle yaparız.
@Override
public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  ...
}
OnStart metodu - Sticky Servis
Normalde servisler işletim sistemi tarafından askıya alınırlar (suspend). Hiç askıya alınmayan servis şöyle başlatılır
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  handleCommand(intent);
  // We want this service to continue running until it is explicitly
  // stopped, so return sticky.
  return START_STICKY;
}
OnStart metodu - Sticky Olmaya Servis
Şöyle yaparız.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  return START_NOT_STICKY;
}
OnTaskRemoved metodu
Açıklaması şöyle. Servis'in ait olduğu uygulama arka plandayken kullanıcı tarafından öldürülürse çağrılır.
This is called if the service is currently running and the user has removed a task that comes from the service's application.
İmzası şöyle.
@Override
public void onTaskRemoved(Intent rootIntent);


Hiç yorum yok:

Yorum Gönder