難道我學的是廣播?(今天真的多圖)

什麼是廣播?

安卓的四大組件之一,是一種廣泛應用在應用程序之間傳輸信息的機制。

什麼是安卓的BroadcastReceiver?

是對發送出來的廣播進行過濾接收並響應的一類組件,它就是用來接收來自系統和應用中的廣播。

怎麼理解Broadcast和BroadcastReceiver ?

Broadcast就像現實中的廣播電臺,他發廣播信號來,然後我們用收音機來接收,然後處理,並且播放出聲音, BroadcastReceiver就相當於那臺收音機。

靜態註冊呢


還有吧

endbroadcastdemo
<?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.lenovo.sendbroadcastdemo.MainActivity">

<Button
    android:id="@+id/call_zfb_btn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="你好支付寶"
    />

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.sendbroadcastdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="微信"
        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>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <permission android:name="zhiyouxiaohuacainengjieshouguangbo"/>
</manifest>

package com.example.lenovo.sendbroadcastdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button.findViewById(R.id.call_zfb_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("zhifubao");
                sendOrderedBroadcast(intent,null);
            }
        });
    }
}
receivebroadcastdemo

package com.example.lenovo.receivebroadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by lenovo on 2018/3/26.
 */

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "叫我幹嘛", Toast.LENGTH_SHORT).show();
        Log.e("Receiver1","1111111111111");
    }
}

package com.example.lenovo.receivebroadcastdemo;

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private  MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("zhifubao");

        registerReceiver(receiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.receivebroadcastdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="支付寶Demo"
        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>
 MyReceiver2
package com.example.lenovo.receivebroadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by lenovo on 2018/3/26.
 */

public class MyReceiver2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"Receiver2接受消息",Toast.LENGTH_LONG).show();
        Log.e("Receiver2","2222222");
        abortBroadcast();
    }
}
<receiver android:name=".MyReceiver">
    <intent-filter android:priority="1000">
        <action android:name="zhifubao">
        </action>
    </intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
    <intent-filter android:priority="-1000">
        <action android:name="zhifubao"/>
    </intent-filter>
</receiver>

呦呦舒服啊,寫完了,明天見。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章