Android學(xué)習(xí)之本地廣播使用方法詳解
本地廣播信息只能在應(yīng)用程序內(nèi)部傳遞,同時廣播接收器也只能接收應(yīng)用程序內(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)方法獲取實(shí)例,注冊廣播消息時是調(diào)用localBroadcastManager實(shí)例的registerReceiver(參數(shù)1,參數(shù)2)方法注冊(參數(shù)1是本地廣播接受者,參數(shù)2是過濾器只選擇接收特定的廣播消息),調(diào)用localBroadcastManager實(shí)例的sendBroadcast(Initent initent)方法發(fā)送廣播消息。
MyRecevity
package 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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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="http://schemas.android.com/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="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter
android:priority="100">
<action android:name="com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中BroadcastReceiver(異步接收廣播Intent)的使用
- Android 廣播大全 Intent Action 事件詳解
- Android apk安裝替換卸載廣播的實(shí)現(xiàn)代碼
- Android中的廣播和廣播接收器代碼實(shí)例
- android連接wifi時獲取廣播地址代碼
- Android基于廣播事件機(jī)制實(shí)現(xiàn)簡單定時提醒功能代碼
- Android廣播接收機(jī)制詳細(xì)介紹(附短信接收實(shí)現(xiàn))
- Android廣播接實(shí)現(xiàn)監(jiān)聽電話狀態(tài)(電話的狀態(tài),攔截)
- Android 開機(jī)廣播的使用及配置
- android如何默認(rèn)打開小區(qū)廣播具體實(shí)現(xiàn)
相關(guān)文章
Android 自定義精美界面包含選項(xiàng)菜單 上下文菜單及監(jiān)聽詳解流程
這篇文章主要介紹了一個Android實(shí)例小項(xiàng)目,它包含了選項(xiàng)菜單、上下文菜單及其對應(yīng)的監(jiān)聽事件,它很小,但這部分功能在Android開發(fā)中很常見,需要的朋友來看看吧2021-11-11
Android采取ContentObserver方式自動獲取驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android采取ContentObserver方式自動獲取驗(yàn)證碼,感興趣的小伙伴們可以參考一下2016-08-08
android 關(guān)于webview 加載h5網(wǎng)頁開啟定位的方法
今天小編就為大家分享一篇android 關(guān)于webview 加載h5網(wǎng)頁開啟定位的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android編程實(shí)現(xiàn)可滑動的開關(guān)效果(附demo源碼下載)
這篇文章主要介紹了Android編程實(shí)現(xiàn)可滑動的開關(guān)效果,涉及Android的布局與控件設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04
Android TV 焦點(diǎn)框移動的實(shí)現(xiàn)方法
本篇文章主要介紹了Android TV 焦點(diǎn)框移動的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

