



下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中怎么使用本地廣播
這篇文章給大家介紹Android中怎么使用本地廣播,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。MainActivity代碼package
com.example.luobo.mybroadcastreceiver;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.content.IntentFilter;
import
android.support.v4.content.LocalBroadcastManager;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener{
private
Button
button;
private
IntentFilter
intentFilter;
private
LocalBroadcastManager
localBroadcastManager
;
private
LocalReceiver
localReciiver;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button
=
(Button)findViewById(R.id.send_button);
button.setOnClickListener(this);
localBroadcastManager
=
LocalBroadcastManager.getInstance(this);//使用
intentFilter
=
new
IntentFilter();
intentFilter.addAction("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
localReciiver
=
new
LocalReceiver();
localBroadcastManager.registerReceiver(localReciiver,intentFilter);
}
@Override
protected
void
onDestroy()
{
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReciiver);
}
@Override
public
void
onClick(View
view)
{
Intent
intent
=
new
Intent("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
}
class
LocalReceiver
extends
BroadcastReceiver{
@Override
public
void
onReceive(Context
context,
Intent
intent)
{
Toast.makeText(context,"received
local
broadcast",Toast.LENGTH_SHORT).show();
}
}
}首先通過(guò)LocalBroadcastManager(本地廣播管理類)的getInstance(this)方法獲取實(shí)例,注冊(cè)廣播消息時(shí)是調(diào)用localBroadcastManager實(shí)例的registerReceiver(參數(shù)1,參數(shù)2)方法注冊(cè)(參數(shù)1是本地廣播接受者,參數(shù)2是過(guò)濾器只選擇接收特定的廣播消息),調(diào)用localBroadcastManager實(shí)例的sendBroadcast(Initentinitent)方法發(fā)送廣播消息。MyRecevitypackage
com.example.luobo.mybroadcastreceiver;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.widget.Toast;
public
class
MyReceiver
extends
BroadcastReceiver
{
@Override
public
void
onReceive(Context
context,
Intent
intent)
{
Toast.makeText(context,"Received
in
MyBroadCastReceiver",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}activity_main.xml<?xml
version="1.0"
encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.luobo.mybroadcastreceiver.MainActivity">
<Button
android:id="@+id/send_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="發(fā)送廣播"/>
</android.support.constraint.ConstraintLayout>AndroidMainfest.aml<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="/apk/res/android"
package="com.example.luobo.mybroadcastreceiver">
<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="ent.action.MAIN"
/>
<category
android:name="ent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter
android:priority="100">
<action
android:name="co
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 員工賬號(hào)授權(quán)合同范本
- 凈水商業(yè)租賃合同范本
- 賣房臨時(shí)出租合同范例
- 北京農(nóng)村租房合同范本
- 代簽訂投標(biāo)合同范本
- 雙方購(gòu)車合同范本
- 單位窗簾裝修合同范例
- 代購(gòu)電纜合同范本
- 廠地購(gòu)買合同范本
- 吊車購(gòu)銷合同范本
- QQ三國(guó)副職及日常物品成本計(jì)算表v
- 中國(guó)傳統(tǒng)故事英文哪吒鬧海二篇
- GB/T 9441-1988球墨鑄鐵金相檢驗(yàn)
- GB/T 4294-1997氫氧化鋁
- GB/T 37862-2019非開(kāi)挖修復(fù)用塑料管道總則
- GB/T 24338.1-2018軌道交通電磁兼容第1部分:總則
- 2023年新改版教科版六年級(jí)下冊(cè)科學(xué)全冊(cè)課件
- 無(wú)人機(jī)測(cè)繪技術(shù)及應(yīng)用課件
- 浦發(fā)銀行個(gè)人信用報(bào)告異議申請(qǐng)表
- 初二數(shù)學(xué)初一數(shù)學(xué)競(jìng)賽講座116講
- 2022暖通空調(diào)第三版課后題答案
評(píng)論
0/150
提交評(píng)論