Android廣播接收器Broadcast Receiver-android學習之旅(十二)

來源:互聯網
上載者:User

標籤:android

首先繼承BroadcastReceiver類,並在manifest中註冊
public class MyReceiver extends BroadcastReceiver {    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        throw new UnsupportedOperationException("Not yet implemented");    }}

在mainifest中註冊

<receiver            android:name=".MyReceiver"            android:enabled="true"            android:exported="true" >        </receiver>
動態註冊和取消廣播接收器

上代碼:
Receiver部分:

public class MyReceiver extends BroadcastReceiver {    public static final String ACTION = "peng.liu.testview.intent.action.MyReceiver";    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        System.out.println(intent.getStringExtra("data")+"hello");    }}

主類部分:

public class MainActivity extends Activity implements View.OnClickListener{    private MyReceiver receiver = null;    private Button send,reg,unReg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.send).setOnClickListener(this);        findViewById(R.id.reg).setOnClickListener(this);        findViewById(R.id.unReg).setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.send:                Intent intent = new Intent(MyReceiver.ACTION);                intent.putExtra("data","jiekxueyuan");                sendBroadcast(intent);                break;            case R.id.reg:                if (receiver == null){                    receiver = new MyReceiver();                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));                }                break;            case R.id.unReg:                if (receiver != null){                    unregisterReceiver(receiver);                    receiver = null;                }                break;        }    }}

布局代碼:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity"    android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="reg"        android:id="@+id/reg" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="unReg"        android:id="@+id/unReg" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="send"        android:id="@+id/send" /></LinearLayout>
廣播的優先順序

這次我們在manifest中靜態註冊

<receiver            android:name=".MyReceiver"            android:enabled="true"            android:exported="true" >            <intent-filter android:priority="8">                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>            </intent-filter>        </receiver>        <receiver            android:name=".MyReceiver2"            android:enabled="true"            android:exported="true" >            <intent-filter android:priority="9">                <action android:name="peng.liu.testview.intent.action.MyReceiver"/>            </intent-filter>        </receiver>

android:priority:用於設定優先權,數字越大,優先順序越高。

高優先順序的終端廣播
//發送部分注意是發送sendOrderedBroadcast(intent,null);Intent intent = new Intent(MyReceiver.ACTION);                intent.putExtra("data","jiekxueyuan");                sendOrderedBroadcast(intent,null);

接收部分

public class MyReceiver2 extends BroadcastReceiver {    public MyReceiver2() {    }    @Override    public void onReceive(Context context, Intent intent) {        System.out.println(intent.getStringExtra("data"));        //這一戶用於中斷後面的低優先順序的接受        abortBroadcast();    }}

Android廣播接收器Broadcast Receiver-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.