Android Application Development Cookbook: 93 Recipes for Building Winning Apps



Download 32,34 Mb.
Pdf ko'rish
bet27/56
Sana26.02.2022
Hajmi32,34 Mb.
#470540
1   ...   23   24   25   26   27   28   29   30   ...   56
Bog'liq
Android Application Development Cookbook

public void onResume() {
super.onResume();
//---register the receiver---
registerReceiver(myReceiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
//---unregister the receiver---
unregisterReceiver(myReceiver);
}
www.it-ebooks.info


 
Recipe 1.4 sending and Receiving Broadcasts

15
public void onClick(View view) {
Intent i = new Intent("MY_SPECIFIC_ACTION");
i.putExtra("key", "some value from intent");
sendBroadcast(i);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent i) {
Toast.makeText(context, 
"Received broadcast in MyBroadcastReceiver, " +
" value received: " + i.getStringExtra("key"),
Toast.LENGTH_LONG).show();
}
}
}
The preceding code snippet shows the inner class 
MyBroadcastReceiver
extending from the 
BroadcastReceiver
base class. In this class, you need to override the 
onReceive()
method so that 
when the broadcast is received, you can perform the action that you want to perform. To get the 
data that is passed to the receiver, you can make use of the 
Intent
object in the second argument
of the 
onReceive()
method. 
To use this class, you need to create an instance of it, as well as create an 
IntentFilter
object:
myReceiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter("MY_SPECIFIC_ACTION");
You specify a user-defined action in the 
IntentFilter
’s constructor, and use your own string to 
define this action. 
To register the 
BroadcastReceiver
object, use the 
registerReceiver()
method, passing it the 
BroadcastReceiver
object as well as the 
IntentFilter
object:
registerReceiver(myReceiver, intentFilter);
Now that you have registered a 
BroadcastReceiver
object, you can send a broadcast to test 
whether it works. To send a broadcast, you use the 
sendBroadcast()
method, passing it an 
Intent
object:
public void onClick(View view) {
Intent i = new Intent("MY_SPECIFIC_ACTION");
i.putExtra("key", "some value from intent");
sendBroadcast(i);
}
If you want to pass data to the receiver, you can use the 
putExra()
method. To unregister the 
broadcast receiver, use the 
unregisterReceiver()
method:
unregisterReceiver(myReceiver);
www.it-ebooks.info



Download 32,34 Mb.

Do'stlaringiz bilan baham:
1   ...   23   24   25   26   27   28   29   30   ...   56




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish