android 廣播機制

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

  Android廣播機制指的是,在一個應用程式啟動並執行時候可以自訂一個訊息類型,讓相應的接收器去處理這個訊息或者是系統訊息,比如來電話了、來簡訊了、手機沒電了等等系統發送的訊息;

  Android提供了兩種註冊廣播接受者的形式,分別是在程式中動態註冊和在xml中指定。他們之間的區別就是作用的範圍不同,程式動態註冊的接收者只在程式運行過程中有效,而在xml註冊的接收者不管你的程式有沒有啟動都會起作用;

 

基於XML配置的註冊廣播形式為:

 

點擊發送按鈕,列印資訊為:

 

代碼如下:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xiaozhang.broadcast1.MainActivity" >    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="點擊發送" /></RelativeLayout>

 

MainActivity.java

package com.xiaozhang.broadcast1;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button sendButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendButton = (Button) findViewById(R.id.button);        sendButton.setOnClickListener(new BroadcastListener());    }    class BroadcastListener implements OnClickListener {        @Override        public void onClick(View v) {            Intent intent = new Intent();            intent.setAction(Intent.ACTION_EDIT);            MainActivity.this.sendBroadcast(intent);        }    }}

 

TestReceiver.java

package com.xiaozhang.broadcast1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class TestReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        System.out.println("接收到了資訊:" + action);    }}

 

在AndroidManifest.xml中配置為:

在<application>中加入:

<receiver android:name=".TestReceiver" >            <intent-filter>                <action android:name="android.intent.action.EDIT" />            </intent-filter>        </receiver>

 

 

在程式中動態指定為:

直接點擊發送廣播,沒反應,要先註冊,再發送,再取消註冊;

 

代碼實現:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.xiaozhang.broadcast2.MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="註冊廣播" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/button1"        android:text="發送廣播" />    <Button        android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/button2"        android:text="取消註冊" /></RelativeLayout>

MainActivity.java

package com.xiaozhang.broadcast2;import java.util.Calendar;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button button1;    private Button button2;    private Button button3;    private ReceiveBroadCast receiveBroadCast = null;    private static final String ACTION = "android:provider.Telephony.SMS_RECEIVE";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        button3 = (Button) findViewById(R.id.button3);        button1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                receiveBroadCast = new ReceiveBroadCast();                IntentFilter filter = new IntentFilter();                filter.addAction(ACTION);                registerReceiver(receiveBroadCast, filter);            }        });        button2.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.putExtra("data", "message: "                        + Calendar.getInstance().get(Calendar.SECOND));                intent.setAction(ACTION);                sendBroadcast(intent);            }        });        button3.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                unregisterReceiver(receiveBroadCast);            }        });    }}

 

ReceiveBroadCast.java

package com.xiaozhang.broadcast2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class ReceiveBroadCast extends BroadcastReceiver {    private static final String TAG = "MyReceiver";    @Override    public void onReceive(Context context, Intent intent) {        String message = intent.getStringExtra("data");        Log.i(TAG, message);    }}

 

推薦閱讀:

http://www.cnblogs.com/qianlifeng/archive/2011/03/06/1972305.html

http://blog.csdn.net/abc5382334/article/details/15796823

http://www.cnblogs.com/totem1990/archive/2012/09/10/2679391.html

android 廣播機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.