Bugünki dersimizde ilk servis çeşiti olan started service konusuna giriş yapıyoruz.
Started service; main thread’de çalışan ve de onu çağıran component’ten (activity, broadcat receiver, service veya content provider) bağımsız olarak çalışan servis türüdür.
Started service; onu çağıran component’ten veri alabilir ama geriye herhangi bir şey göndermez. Bu yüzden onBing methodu null değer geriye gönderir. Eğer geriye veri göndermek istiyorsak ilerleyen derslerde göreceğimiz BpundService kullanmamız veya bir Receiver implemente etmemiz gerekir.
Aynı bir activity veya broadcast receiver başlatırken kullandığımız gibi, service başlatırken de intentlerden faydalanırız. Oluşturduğumuz intenti startService() methoduna parametre olarak göndererek servisimizi başlatabiliriz.Servisi durdururken de yine intent nesnesini kullanarak stopService methoduna göndeririz. Servis sınfı içinden servisi durdurmak için stopSelf() methodu da kullanılabilir. Bu methodu kullanmak için parametre olarak intent nesnesi göndermemiz gerekmez. Tabiki de ilgili servisi manifest dosyasına belirtmeyi de unutmamamız gerekiyor.
Bir service başlatıldığında öncelikle onCreate methodu ile servis yaratılır.Servis sınıfı içerisindeki onStartCommand methodunda yapacağımız işlemleri belirtiriz. Servis durdurulduğunda ise onDestroy methodu tetiklenir.
Daha fazla açıklamayı videoda bulabilirsiniz. Bu derste yazılan kodlar aşağıdaki gibidir:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void servisBaslat(View view) { Intent intent=new Intent(MainActivity.this, MyStartedService.class); startService(intent); } public void servisDurdur(View view) { Intent intent=new Intent(MainActivity.this, MyStartedService.class); stopService(intent); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class MyStartedService extends Service { MediaPlayer player; private static final String TAG = MyStartedService.class.getSimpleName(); @Override public void onCreate() { Log.i(TAG, "OnCreate Çağrıldı " + " " + Thread.currentThread().getName()); super.onCreate(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "OnStartCommand Çağrıldı " + " " + Thread.currentThread().getName()); /* try { Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); }*/ player = MediaPlayer.create(MyStartedService.this, R.raw.explosion); player.setLooping(true); player.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.i(TAG, "OnDestroy Çağrıldı " + " " + Thread.currentThread().getName()); player.stop(); super.onDestroy(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.emrealtunbilek.startedserviceornek.MainActivity"> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="CheckBox" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="Start Service" android:onClick="servisBaslat" app:layout_constraintLeft_toLeftOf="parent" tools:layout_editor_absoluteY="75dp" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_below="@+id/checkBox" android:layout_alignStart="@+id/checkBox" android:layout_marginTop="38dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/button" android:layout_below="@+id/button" android:onClick="servisDurdur" android:layout_marginTop="31dp" android:text="Stop Service" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.emrealtunbilek.startedserviceornek"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyStartedService"></service> </application> </manifest> |