国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之BroadcastReceiver用法實例分析

2020-04-11 11:32:17
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發中BroadcastReceiver用法。分享給大家供大家參考。具體分析如下:

在Android系統中,廣播(Broadcast)是在組件之間傳播數據(Intent)的一種機制。

Braodcast Receiver顧名思義就是廣播接收器,它和事件處理機制類似,但是事件處理機制是程序組件級別的(比如:按鈕的單擊事件),而廣播事件處理機制是系統級別的。我們可以用Intent來啟動一個組件,也可以用sendBroadcast()方法發起一個系統級別的事件廣播來傳遞消息。我們同樣可以在自己的應用程序中實現Broadcast Receiver來監聽和響應廣播的Intent。

事件的廣播通過創建Intent對象并調用sendBroadcast()方法將廣播發出。事件的接受是通過定義一個繼承BroadcastReceiver的類來實現的,繼承該類后覆蓋其onReceive()方法,在該方法中響應事件。

下面是android系統中定義了很多標準的Broadcast Action來響應系統的廣播事件。

①ACTION_TIME_CHANGED(時間改變時觸發)
②ACTION_BOOT_COMPLETED(系統啟動完成后觸發)--比如有些程序開機后啟動就是用這種方式來實現的
③ACTION_PACKAGE_ADDED(添加包時觸發)
④ACTION_BATTERY_CHANGED(電量低時觸發)

下面看一個例子:

我們在一個按鈕上綁定一個事件,事件通過發送一個廣播來觸發logcat打出一個log。

先看manifest文件。

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.example.test"   android:versionCode="1"   android:versionName="1.0" >   <uses-sdk     android:minSdkVersion="8"     android:targetSdkVersion="16" />  <application     android:allowBackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/AppTheme" >    <activity       android:name="com.example.broadcast.BroadcastReceiverActivity"       android:label="@string/app_name_bc" >      <intent-filter>        <action android:name="android.intent.action.MAIN" >        </action>        <category android:name="android.intent.category.LAUNCHER" >        </category>      </intent-filter>    </activity>    <receiver android:name="com.example.broadcast.HelloBroadReciever" >      <intent-filter>        <action android:name="comz.test.printlog" >        </action>      </intent-filter>    </receiver>  </application></manifest>

上面聲明了一個receiver。接收名字是comz.test.printlog的消息。

看activity:

package com.example.broadcast;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import com.example.test.R;public class BroadcastReceiverActivity extends Activity {  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Button b1 = (Button) findViewById(R.id.broadcastBtn);    b1.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        Log.e("mason", "here");        // 定義一個intent        Intent intent = new Intent().setAction("comz.test.printlog")            .putExtra("info", "here is your info.");        // 廣播出去        sendBroadcast(intent);      }    });  }}

在這段代碼中,定義一個intent并發送廣播出去。

看BroadReceiver的代碼:

package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class HelloBroadReciever extends BroadcastReceiver {  // 如果接收的事件發生  @Override  public void onReceive(Context context, Intent intent) {    Log.e("mason", "on receive");    if (intent.getAction().equals("comz.test.printlog")) {      Log.e("mason", intent.getStringExtra("info"));    }  }}

這是BroadcastReceiver的代碼。

在接收到消息之后,如果消息是comz.test.printlog,則打印消息。

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湾仔区| 红河县| 婺源县| 印江| 凉城县| 黔西县| 肃宁县| 射洪县| 台州市| 德格县| 临沧市| 南丰县| 广饶县| 兖州市| 拜城县| 晴隆县| 和顺县| 湟中县| 临沧市| 长白| 萍乡市| 化州市| 余姚市| 遂宁市| 神池县| 开原市| 宣武区| 武川县| 临沭县| 固阳县| 元阳县| 禹城市| 苏尼特左旗| 哈尔滨市| 浦江县| 赤城县| 长垣县| 澄城县| 凤翔县| 织金县| 旌德县|