付費下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應用開發(fā)技術(shù)】Android中怎么使用本地廣播
這篇文章給大家介紹Android中怎么使用本地廣播,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。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();
}
}
}首先通過LocalBroadcastManager(本地廣播管理類)的getInstance(this)方法獲取實例,注冊廣播消息時是調(diào)用localBroadcastManager實例的registerReceiver(參數(shù)1,參數(shù)2)方法注冊(參數(shù)1是本地廣播接受者,參數(shù)2是過濾器只選擇接收特定的廣播消息),調(diào)用localBroadcastManager實例的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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《音階歌(唱游、發(fā)現(xiàn))》教學設(shè)計-2025-2026學年接力版(新教材)小學音樂一年級下冊
- 甲殼類養(yǎng)殖工安全實操測試考核試卷含答案
- 氣體凈化工崗前理論技能考核試卷含答案
- 碾泥工安全檢查測試考核試卷含答案
- 我國上市公司治理結(jié)構(gòu)的理性構(gòu)建之路
- 我國上市公司并購重組盈利補償機制:實踐、問題與優(yōu)化路徑
- 丁苯橡膠裝置操作工安全應急知識考核試卷含答案
- 苗木培育工崗前安全理論考核試卷含答案
- 鎢絞絲加熱子制造工安全綜合考核試卷含答案
- 水工混凝土維修工達標能力考核試卷含答案
- 完整工資表模板(帶公式)
- 家長要求學校換老師的申請書
- 奇瑞汽車QC小組成果匯報材料
- 闌尾腫瘤-課件
- CTT2000LM用戶手冊(維護分冊)
- 川2020J146-TJ 建筑用輕質(zhì)隔墻條板構(gòu)造圖集
- 正式員工派遣單
- 新員工入職申請表模板
- 中外新聞事業(yè)史課程教學大綱
- LY/T 1357-2008歧化松香
- 化工廠常見隱患危害因素及防范措施
評論
0/150
提交評論