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

Android學習之本地廣播使用方法詳解

 更新時間:2017年08月09日 10:06:26   作者:天秤心已隨風去  
這篇文章主要為大家詳細介紹了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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

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

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

    Android使用貝塞爾曲線畫心形

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

    TableLayout(表格布局)基礎知識點詳解

    在本文里我們給大家分享了關(guān)于TableLayout(表格布局)的相關(guān)基礎知識點內(nèi)容,需要的朋友們學習下。
    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 點擊無響應的解決辦法

    iOS UIButton 點擊無響應的解決辦法

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

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

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

最新評論

荣昌县| 临潭县| 塔城市| 余江县| 兰西县| 交城县| 柯坪县| 丹寨县| 长乐市| 姜堰市| 牟定县| 定日县| 东宁县| 东丽区| 镇原县| 忻城县| 宝坻区| 郯城县| 丽江市| 昌邑市| 玉田县| 鄂伦春自治旗| 呼玛县| 怀化市| 博湖县| 牡丹江市| 内江市| 许昌县| 新竹市| 德州市| 龙海市| 临海市| 永胜县| 施秉县| 甘洛县| 贵德县| 赤城县| 家居| 游戏| 招远市| 莱阳市|