Blog

Android – Broadcast Receivers

Uncategorized @uz

Android – Broadcast Receivers

Broadcast Receivers shunchaki boshqa dasturlardan yoki tizimning o’zi tomonidan yuborilgan xabarlarga javob beradi. Ushbu xabarlar bir muncha vaqt voqealar yoki niyatlar deb nomlanadi. Masalan, dasturlar boshqa dasturlarning qurilmaga yuklab olinganligi va ulardan foydalanishlari mumkinligi to’g’risida boshqa dasturlarga xabar berish uchun translyatsiyalarni boshlashi mumkin, shuning uchun ushbu aloqani to’xtatadigan va tegishli harakatlarni boshlaydigan eshittirish qabul qiluvchisi.

BroadcastReceiver-ni tizimga uzatiladigan maqsadlar uchun ishlashi uchun quyidagi ikkita muhim qadam mavjud:

  • Efir qabul qiluvchisini yaratish.
  • Eshittirish qabul qiluvchisini ro’yxatdan o’tkazish

Agar siz o’zingizning shaxsiy maqsadlaringizni amalga oshirmoqchi bo’lsangiz, yana bitta qo’shimcha qadam mavjud, keyin siz ushbu niyatlarni yaratishingiz va efirga uzatishingiz kerak bo’ladi.

Efir qabul qiluvchisini yaratish

Eshittirish qabul qiluvchisi BroadcastReceiver sinfining subklassi sifatida amalga oshiriladi va onReceive () usulini bekor qiladi, bu erda har bir xabar Intent ob’ekti parametri sifatida qabul qilinadi .

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

Eshittirish qabul qiluvchisini ro’yxatdan o’tkazish

Ilova AndroidManifest.xml faylida eshittirish qabul qiluvchisini ro’yxatdan o’tkazish orqali ma’lum bir translyatsiya maqsadlarini tinglaydi . MyReceiver- ni tizim tomonidan ishlab chiqarilgan ACTION_BOOT_COMPLETED hodisasi uchun ro’yxatdan o’tkazishni o’ylaymiz , Android tizimi yuklash jarayonini tugatgandan so’ng tizim tomonidan o’chiriladi.

translyatsiya

Eshittirish-qabul qiluvchi

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
   
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
         </action>
      </intent-filter>
   
   </receiver>
</application>

Endi har qanday Android qurilmangiz yuklansa , u BroadcastReceiver MyReceiver tomonidan ushlanib qoladi va onReceive () ichidagi mantiq bajariladi.

Intent sinfida yakuniy statik maydonlar sifatida aniqlangan bir nechta tizim tomonidan yaratilgan hodisalar mavjud . Quyidagi jadvalda bir nechta muhim tizim voqealari keltirilgan.

NVoqealar doimiy va tavsifi
1android.intent.action.BATTERY_CHANGED

Batareyaning quvvat darajasi, darajasi va boshqa ma’lumotlarni o’z ichiga olgan yopishqoq eshittirish.

2android.intent.action.BATTERY_LOW

Qurilmada batareyaning kamligi ko’rsatilgan.

3android.intent.action.BATTERY_OKAY

Batareya quvvati past bo’lganidan keyin endi yaxshi ekanligini bildiradi.

4android.intent.action.BOOT_COMPLETED

Bu tizim yuklashni tugatgandan so’ng, bir marta translyatsiya qilinadi.

5android.intent.action.BUG_REPORT

Xato haqida xabar berish uchun harakatlarni ko’rsating.

6android.intent.action.CALL

Ma’lumotlar bilan ko’rsatilgan kishiga qo’ng’iroq qiling.

7android.intent.action.CALL_BUTTON

Qo’ng’iroq qilish uchun foydalanuvchi raqamga yoki boshqa tegishli foydalanuvchi interfeysiga o’tish uchun foydalanuvchi «qo’ng’iroq» tugmachasini bosdi.

8android.intent.action.DATE_CHANGED

Sana o’zgartirildi.

9android.intent.action.REBOOT

Qurilmani qayta yoqing.

Maxsus niyatlarni eshittirish

Agar siz o’zingizning dasturingiz maxsus niyatlarni yaratishi va yuborishini istasangiz, unda siz o’zingizning faoliyat sinfingiz ichida sendBroadcast () usuli yordamida ushbu niyatlarni yaratishingiz va yuborishingiz kerak bo’ladi . Agar siz sendStickyBroadcast (Intent) usulidan foydalansangiz, Niyat yopishqoq bo’ladi , ya’ni siz yuborayotgan Niyat translyatsiya tugagandan so’ng qoladi.

public void broadcastIntent(View view) {
   Intent intent = new Intent();
   intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
   sendBroadcast(intent);
}

Ushbu maqsad com.tutorialspoint.CUSTOM_INTENT ham xuddi shunday tarzda ro’yxatdan o’tkazilishi mumkin, chunki biz tizimni qayta tuzilgan niyat bilan yaratdik.

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
   
      <intent-filter>
         <action android:name="com.tutorialspoint.CUSTOM_INTENT">
         </action>
      </intent-filter>
   
   </receiver>
</application>

Misol

Ushbu misol sizga BroadcastReceiver- ni maxsus niyatni to’xtatish uchun qanday yaratishni tushuntiradi . Maxsus niyat bilan tanishib chiqqandan so’ng, siz o’zingizning dasturingizni tizim tomonidan yaratilgan niyatlarni ushlab turish uchun dasturlashingiz mumkin. Keling, Hello World Example bobida yaratgan Android dasturini o’zgartirish uchun quyidagi amallarni bajaring –

QadamTavsif
1Siz Android dasturini yaratish uchun Android studiyasidan foydalanasiz va uni » Hello World Example» bobida aytib o’tilganidek, com.example.tutorialspoint7.myapplication to’plami ostida » Mening ilovam» deb nomlaysiz .
2BroadIntent () usulini qo’shish uchun MainActivity.java asosiy faoliyat faylini o’zgartiring .
3Com.example.tutorialspoint7.myapplication to’plami ostida BroadcastReceiver-ni aniqlash uchun MyReceiver.java nomli yangi java faylini yarating .
4Ilova bir yoki bir nechta maxsus va tizim maqsadlarini hech qanday cheklovlarsiz bajarishi mumkin. Siz ushlamoqchi bo’lgan har qanday maqsad Android qabul qiluvchisi / / yorlig’i yordamida AndroidManifest.xml faylida ro’yxatdan o’tkazilishi kerak.
5Res / layout / activity_main.xml faylining standart tarkibini efirga uzatish tugmachasini qo’shish uchun o’zgartiring.
6String faylini o’zgartirishga hojat yo’q, Android studio string.xml faylini parvarish 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. Maxsus niyatni translyatsiya qilish uchun BroadIntent () usulini qo’shdik .

package com.example.tutorialspoint7.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

   /** Called when the activity is first created. */
   @Override
   
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }


   // broadcast a custom intent.
      
   public void broadcastIntent(View view){
      Intent intent = new Intent();
      intent.setAction("com.tutorialspoint.CUSTOM_INTENT"); sendBroadcast(intent);
   }
}

MyReceiver.java- ning mazmuni quyidagicha :

package com.example.tutorialspoint7.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by TutorialsPoint7 on 8/23/2016.
 */
public class MyReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

Quyidagi AndroidManifest.xml faylining o’zgartirilgan tarkibi bo’ladi . Bizning xizmatimizga <qabul qiluvchi … /> 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>
   
      <receiver android:name="MyReceiver">
         <intent-filter>
            <action android:name="com.tutorialspoint.CUSTOM_INTENT">
            </action>
         </intent-filter>

      </receiver>
   </application>

</manifest>

Res / layout / activity_main.xml faylining mazmuni quyidagicha bo’ladi, bu bizning maxsus niyatimizni tarqatish tugmachasini 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 Broadcast"
      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="Broadcast Intent"
      android:onClick="broadcastIntent"
      android:layout_below="@+id/imageButton"
      android:layout_centerHorizontal="true" />

</RelativeLayout>

O’zgartirilgan Salom Dunyomizni ishga tushirishga harakat qilaylik ! dasturni biz o’zgartirdik. O’ylaymanki, siz o’zingizning AVD- ni atrof-muhitni sozlash paytida yaratgansiz . Ilovani Android studiyasidan ishga tushirish uchun loyihangizning faollik fayllaridan birini oching va Eclipse Run 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 Broadcast Demo

Endi bizning maxsus niyatimizni translyatsiya qilish uchun Broadcast Niyat tugmachasini bosamiz, bu bizning «com.tutorialspoint.CUSTOM_INTENT» maxsus niyatimizni uzatadi, bu bizning ro’yxatdan o’tgan BroadcastReceiver, ya’ni MyReceiver tomonidan ushlab turiladi va bizning mantiqimizga muvofiq pastki qismida tushdi paydo bo’ladi. simulyatordan quyidagicha –

Android Broadcast Niyat

Siz tizimni ishga tushirish, o’zgartirish sanasi, batareyaning kamligi va boshqalar kabi tizim tomonidan yaratilgan niyatlarni to’xtatish uchun boshqa BroadcastReceiver dasturini sinab ko’rishingiz 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