Android學習之本地廣播使用方法詳解
本地廣播信息只能在應用程序內(nèi)部傳遞,同時廣播接收器也只能接收應用程序內(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(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android中BroadcastReceiver(異步接收廣播Intent)的使用
- Android 廣播大全 Intent Action 事件詳解
- Android apk安裝替換卸載廣播的實現(xiàn)代碼
- Android中的廣播和廣播接收器代碼實例
- android連接wifi時獲取廣播地址代碼
- Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼
- Android廣播接收機制詳細介紹(附短信接收實現(xiàn))
- Android廣播接實現(xiàn)監(jiān)聽電話狀態(tài)(電話的狀態(tài),攔截)
- Android 開機廣播的使用及配置
- android如何默認打開小區(qū)廣播具體實現(xiàn)
相關(guān)文章
Android 中ScrollView與ListView沖突問題的解決辦法
這篇文章主要介紹了Android 中ScrollView與ListView沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握解決問題的辦法,需要的朋友可以參考下2017-10-10
Android 讓自定義TextView的drawableLeft與文本一起居中
本文主要介紹Android 自定義控件TextView顯示居中問題,在開發(fā)過程中經(jīng)常會遇到控件的重寫,這里主要介紹TextView的drawableLeft與文本一起居中的問題2016-07-07
Android webview與js的數(shù)據(jù)交互
有了WebView這個組件,Android應用開發(fā)技術(shù)也就轉(zhuǎn)嫁到html與java數(shù)據(jù)交互上來。說白了就是js與WebView的數(shù)據(jù)交互,這就是本文所要討論的2017-04-04

