最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android學(xué)習(xí)之本地廣播使用方法詳解

 更新時(shí)間:2017年08月09日 10:06:26   作者:天秤心已隨風(fēng)去  
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)之本地廣播使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本地廣播信息只能在應(yīng)用程序內(nèi)部傳遞,同時(shí)廣播接收器也只能接收應(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í)例,注冊廣播消息時(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 中ScrollView與ListView沖突問題的解決辦法

    Android 中ScrollView與ListView沖突問題的解決辦法

    這篇文章主要介紹了Android 中ScrollView與ListView沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握解決問題的辦法,需要的朋友可以參考下
    2017-10-10
  • Android 簡單跳轉(zhuǎn)頁面工具的實(shí)例詳解

    Android 簡單跳轉(zhuǎn)頁面工具的實(shí)例詳解

    這篇文章主要介紹了Android 簡單跳轉(zhuǎn)頁面工具的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Android生成二維碼工具類封裝及使用

    Android生成二維碼工具類封裝及使用

    最近公司業(yè)務(wù)上有個(gè)生成二維碼圖片的需求(Android端),之后筆者在網(wǎng)上查閱了一些資料,實(shí)現(xiàn)了這個(gè)功能,這篇文章主要給大家介紹了關(guān)于Android生成二維碼工具類封裝及使用的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • 淺扒Android動態(tài)設(shè)置字體大小的示例

    淺扒Android動態(tài)設(shè)置字體大小的示例

    本篇文章主要介紹了淺扒Android動態(tài)設(shè)置字體大小的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Android使用貝塞爾曲線畫心形

    Android使用貝塞爾曲線畫心形

    這篇文章主要為大家詳細(xì)介紹了Android使用貝塞爾曲線畫心形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • TableLayout(表格布局)基礎(chǔ)知識點(diǎn)詳解

    TableLayout(表格布局)基礎(chǔ)知識點(diǎn)詳解

    在本文里我們給大家分享了關(guān)于TableLayout(表格布局)的相關(guān)基礎(chǔ)知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • Android 讓自定義TextView的drawableLeft與文本一起居中

    Android 讓自定義TextView的drawableLeft與文本一起居中

    本文主要介紹Android 自定義控件TextView顯示居中問題,在開發(fā)過程中經(jīng)常會遇到控件的重寫,這里主要介紹TextView的drawableLeft與文本一起居中的問題
    2016-07-07
  • Android Bitmap壓縮方法的選擇詳解

    Android Bitmap壓縮方法的選擇詳解

    這篇文章主要介紹了Android Bitmap壓縮方法的選擇的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS UIButton 點(diǎn)擊無響應(yīng)的解決辦法

    iOS UIButton 點(diǎn)擊無響應(yīng)的解決辦法

    在開發(fā)中按鈕我們經(jīng)常會遇到,但是有時(shí)候會碰到一些難以處理的問題,就是按鈕點(diǎn)擊無響應(yīng),其實(shí)解決方法也不難。下面小編之家小編抽空給大家介紹iOS UIButton 點(diǎn)擊無響應(yīng)的解決辦法,需要的朋友參考下吧
    2017-12-12
  • Android webview與js的數(shù)據(jù)交互

    Android webview與js的數(shù)據(jù)交互

    有了WebView這個(gè)組件,Android應(yīng)用開發(fā)技術(shù)也就轉(zhuǎn)嫁到html與java數(shù)據(jù)交互上來。說白了就是js與WebView的數(shù)據(jù)交互,這就是本文所要討論的
    2017-04-04

最新評論

广河县| 桂东县| 丽水市| 麻栗坡县| 通榆县| 香港| 苏尼特左旗| 金昌市| 武川县| 新余市| 宝丰县| 金寨县| 瑞金市| 齐齐哈尔市| 呼伦贝尔市| 原平市| 湟源县| 托克逊县| 淳化县| 永城市| 杨浦区| 汉沽区| 西充县| 响水县| 襄汾县| 江孜县| 田阳县| 禄丰县| 彩票| 右玉县| 万年县| 东兰县| 星子县| 铜鼓县| 莆田市| 图们市| 台湾省| 上蔡县| 通河县| 永仁县| 呼伦贝尔市|