Blog

Android – Xizmatlar «Services»

Uncategorized @uz

Android – Xizmatlar «Services»

A xizmati fonda ishlayotgan foydalanuvchi bilan o’zaro kerak holda operatsiyalarini uzoq-yugurib bajarish va ariza vayron bo’lsa ham ishlaydi, bir qismi hisoblanadi. Xizmat asosan ikkita holatni olishi mumkin –

N.Shtat va tavsif
1Started

Xizmat Started dastur komponenti, bunday faoliyat sifatida, qo’ng’iroq uni boshlaydi startService () . Ishga tushirilgandan so’ng, xizmat fonda cheksiz ravishda ishlashi mumkin, hatto uni boshlagan komponent yo’q qilingan bo’lsa ham.

2Bound

Xizmat Bound qachon chaqirib unga dastur komponenti bilan bog’laydi bindService () . Bog’langan xizmat komponentlar xizmat bilan o’zaro aloqada bo’lishiga, so’rovlarni yuborishiga, natijalarni olishiga va hattoki protsesslararo aloqa (IPC) jarayonlari davomida buni amalga oshirishga imkon beradigan mijoz-server interfeysini taklif qiladi.

Xizmatda xizmatning holatidagi o’zgarishlarni kuzatish uchun hayot tsiklini qayta chaqirish usullari mavjud va siz tegishli bosqichda ishlarni bajarishingiz mumkin. Chapdagi quyidagi diagrammada xizmat startService () bilan yaratilgan hayot tsikli ko’rsatilgan va o’ngdagi diagrammada xizmat bindService () bilan yaratilgan hayot tsikli ko’rsatilgan: (rasm iltifot: android.com)

Android xizmatining hayot aylanishi

Xizmatni yaratish uchun siz Service basic sinfini yoki mavjud subklasslardan birini kengaytiradigan Java sinfini yaratasiz. Xizmat bazasi sinf turli qayta qo’ng’iroq usullarini belgilaydi va eng muhim quyida berilgan. Qayta qo’ng’iroq qilishning barcha usullarini amalga oshirishingiz shart emas. Biroq, ularning har birini tushunishingiz va ilovangiz foydalanuvchilar kutganidek ishlashini ta’minlaydigan dasturlarni amalga oshirishingiz muhimdir.

N.Qayta qo’ng’iroq qilish va tavsif
1onStartCommand()

Tizim ushbu usulni boshqa bir komponent, masalan, faoliyat, startService () ga qo’ng’iroq qilib, xizmatni boshlashni talab qilganda chaqiradi . Agar siz ushbu usulni qo’llasangiz, stopSelf () yoki stopService () usullariga qo’ng’iroq qilib, uning ishi tugagandan so’ng xizmatni to’xtatish sizning vazifangizdir .

2onBind()

Tizim bu usulni boshqa tarkibiy qism bindService () ga qo’ng’iroq qilib xizmat bilan bog’lashni xohlaganda chaqiradi . Agar siz ushbu usulni amalga oshirsangiz, IBinder ob’ektini qaytarish orqali mijozlar xizmat bilan aloqa qilish uchun foydalanadigan interfeysni taqdim etishingiz kerak . Siz har doim ushbu usulni amalga oshirishingiz kerak, ammo agar siz bog’lashga ruxsat berishni xohlamasangiz, null qiymatini qaytarishingiz kerak .

3onUnbind()

Tizim ushbu usulni barcha mijozlar xizmat tomonidan e’lon qilingan ma’lum bir interfeysdan uzilib qolganda chaqiradi.

4onRebind ()

Tizim ushbu usulni yangi mijozlar xizmatga ulanganda, avvallari barchasi onUnbind (Intent) da uzilib qolganligi to’g’risida xabar berilgandan so’ng, chaqiradi .

5onCreate ()

Xizmat birinchi marta onStartCommand () yoki onBind () yordamida yaratilganida tizim ushbu usulni chaqiradi . Ushbu qo’ng’iroq bir martalik sozlashni amalga oshirish uchun talab qilinadi.

6onDestroy ()

Xizmat endi ishlatilmaganda va yo’q qilinishda tizim ushbu usulni chaqiradi. Sizning xizmatingiz ushbu mavzular, ro’yxatdan o’tgan tinglovchilar, qabul qiluvchilar va boshqalar kabi barcha manbalarni tozalash uchun amalga oshirishi kerak.

Quyidagi skelet xizmati hayot aylanishining har bir usulini namoyish etadi –

package com.tutorialspoint;

import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.os.Bundle;

public class HelloService extends Service {
   
   /** indicates how to behave if the service is killed */
   int mStartMode;
   
   /** interface for clients that bind */
   IBinder mBinder;     
   
   /** indicates whether onRebind should be used */
   boolean mAllowRebind;

   /** Called when the service is being created. */
   @Override
   public void onCreate() {
     
   }

   /** The service is starting, due to a call to startService() */
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      return mStartMode;
   }

   /** A client is binding to the service with bindService() */
   @Override
   public IBinder onBind(Intent intent) {
      return mBinder;
   }

   /** Called when all clients have unbound with unbindService() */
   @Override
   public boolean onUnbind(Intent intent) {
      return mAllowRebind;
   }

   /** Called when a client is binding to the service with bindService()*/
   @Override
   public void onRebind(Intent intent) {

   }

   /** Called when The service is no longer used and is being destroyed */
   @Override
   public void onDestroy() {

   }
}

Misol

Ushbu misol sizga o’zingizning Android xizmatingizni qanday yaratishni ko’rsatadigan oddiy qadamlardan o’tadi. Biz Hello World Example bobida yaratgan Android dasturini o’zgartirish uchun quyidagi amallarni bajaring –

QadamTavsif
1Android StudioIDE-dan Android ilovasini yaratish va uni Hello World Example bobida aytib o’tilganidek com.example.tutorialspoint7.myapplication to’plami ostida Mening Ilovam deb nomlash uchun ishlatasiz .
2Asosiy faoliyati fayl o’zgartirish MainActivity.java qo’shish startService () va stophizmet () usullari.
3Com.example.My Application to’plami ostida MyService.java yangi java faylini yarating . Ushbu faylda Android xizmatiga tegishli usullar qo’llaniladi.
4<Service … /> yorlig’i yordamida AndroidManifest.xml faylida xizmatingizni aniqlang . Ilovada hech qanday cheklovlarsiz bir yoki bir nechta xizmat bo’lishi mumkin.
5Res / layout / activity_main.xml faylining standart tarkibini chiziqli tartibga ikkita tugmachani kiritish uchun o’zgartiring.
6Res / values ​​/ strings.xml faylida biron bir doimiylikni o’zgartirishga hojat yo’q . Android studiyasi satr qiymatlari haqida g’amxo’rlik qiladi
7Android emulyatorini ishga tushirish va ilovada kiritilgan o’zgarishlar natijasini tekshirish uchun dasturni ishga tushiring.

Quyida MainActivity.java o’zgartirilgan asosiy faoliyat faylining mazmuni keltirilgan . Ushbu fayl hayot tsiklining asosiy usullaridan har birini o’z ichiga olishi mumkin. Biz qo’shgan startService () va stophizmet () boshlash va xizmat to’xtatish usullari.

package com.example.tutorialspoint7.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {
   String msg = "Android : ";

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   public void startService(View view) {
      startService(new Intent(getBaseContext(), MyService.class));
   }

   // Method to stop the service
   public void stopService(View view) {
      stopService(new Intent(getBaseContext(), MyService.class));
   }
}

Quyida MyService.java- ning mazmuni berilgan . Ushbu fayl talablar asosida Servis bilan bog’liq bo’lgan bir yoki bir nechta usullarni amalga oshirishi mumkin. Hozircha onStartCommand () va onDestroy () – faqat ikkita usulni amalga oshirmoqchimiz

package com.example.tutorialspoint7.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
   * Created by TutorialsPoint7 on 8/23/2016.
*/

public class MyService extends Service {
   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }
	
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }
}

Quyidagi AndroidManifest.xml faylining o’zgartirilgan tarkibi bo’ladi . Bizning xizmatimizga <service … /> yorlig’ini qo’shdik –

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.tutorialspoint7.myapplication">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      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=".MyService" />
   </application>

</manifest>

Res / layout / activity_main.xml faylining tarkibi quyidagi ikkita tugmachani o’z ichiga oladi –

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Example of services"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_above="@+id/imageButton"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="40dp" />

   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button2"
      android:text="Start Services"
      android:onClick="startService"
      android:layout_below="@+id/imageButton"
      android:layout_centerHorizontal="true" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Stop Services"
      android:id="@+id/button"
      android:onClick="stopService"
      android:layout_below="@+id/button2"
      android:layout_alignLeft="@+id/button2"
      android:layout_alignStart="@+id/button2"
      android:layout_alignRight="@+id/button2"
      android:layout_alignEnd="@+id/button2" />

</RelativeLayout>

O’zgartirilgan Salom Dunyomizni ishga tushirishga harakat qilaylik ! dasturni biz o’zgartirdik. O’ylaymanki, siz atrof-muhitni sozlash paytida AVD- ni yaratgansiz . Ilovani Android studiyasidan ishga tushirish uchun loyihangizning faollik fayllaridan birini oching va Android StudioRun belgisiasboblar panelidan Ishga tushirish belgisini bosing . Android Studio dasturni AVD-ga o’rnatadi va ishga tushiradi, agar sozlamalaringiz va ilovangizda hammasi yaxshi bo’lsa, u quyidagi Emulator oynasini ko’rsatadi –

Android xizmati demo

Endi o’z xizmatingizni ishga tushirish uchun xizmatni ishga tushirish tugmachasini bosamiz, bu xizmatni ishga tushiradi va onStartCommand () usuli bo’yicha dasturlashimizga muvofiq simulyatorning pastki qismida Service Started xabari paydo bo’ladi –

Android xizmati ishga tushirildi

Xizmatni to’xtatish uchun siz Xizmatni to’xtatish tugmachasini bosishingiz mumkin.

Leave your thought here

Email manzilingiz chop etilmaydi. Majburiy bandlar * bilan belgilangan

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Compare
Wishlist 0
Open wishlist page Continue shopping