Android学习总结——Broadcast

Posted ChaoYoung

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习总结——Broadcast相关的知识,希望对你有一定的参考价值。

一.利用BroadcastReceiever监听短信

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.boradcast">
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyBoradcast">
            <intent-filter>
                <action android:name ="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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.example.boradcast.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

 

MainActivity:
package com.example.boradcast;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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


MyBoradcast:
package com.example.boradcast;

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

/**
 * Created by 杨雄超 on 2016/8/17.
 */
public class MyBoradcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"你有新信息",Toast.LENGTH_SHORT).show();
    }
}

总结:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 去除状态栏和标题栏
     <action android:name ="android.provider.Telephony.SMS_RECEIVED"></action> 广播接受者关注的是什么事件
     android:screenOrientation="landscape" 横屏
 
二.自定义广播的发送和接收
发送:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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.example.broadcastsender.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_StartBroadcast"
        android:text="发送广播"/>
</RelativeLayout>
MainActivity:

package com.example.broadcastsender;

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 implements View.OnClickListener {
    private Button btnStartBroad;

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

        btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast);

        btnStartBroad.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_StartBroadcast:
                Intent intent=new Intent();
                //设置action,便于接收方使用
                intent.setAction("com.xch");
                intent.putExtra("key","发送广播了!");
                //发送广播
                sendBroadcast(intent);
                break;
        }
    }
}

接收:

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".MyBroadcast">
            <intent-filter>
                <!--此处action为发送方设置的action-->
                <action android:name="com.xch"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
MyBroadcast:
package com.example.broadcastreceive;

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

/**
 * Created by 杨雄超 on 2016/8/31.
 */
public class MyBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String str=intent.getExtras().getString("key");
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
    }
}

总结:自定义广播发送和接收可以利用Intent,需要注意的是要设置一个action,便于接收:如下

  intent.setAction("com.xch");

<action android:name="com.xch"/>

 

 结果:

 三: 广播接收者的优先级别

 1.在AndroidManifest.xml中设置优先级别,具体为在intent-filter标签中设置android:priority="1000";这里设置1000,如果我们要让这个接受者为最高级别,则其他接收者级别应该设置得比这个低。
 2.在代码中发送方应该发送有序的广播,之前的sendBroadcast(intent);是无序的,这里我们应该用:sendOrderedBroadcast(intent,null);
 3.在接收方拦截掉比当前优先级别低的广播:abortBroadcast();

具体代码如下:
发送广播:
package com.example.broadcastsender;

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 implements View.OnClickListener {
    private Button btnStartBroad;

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

        btnStartBroad=(Button) findViewById(R.id.btn_StartBroadcast);

        btnStartBroad.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_StartBroadcast:
                Intent intent=new Intent();
                //设置action,便于接收方使用
                intent.setAction("com.xch");
                intent.putExtra("key","发送广播了!");
                //发送无序的广播
                //sendBroadcast(intent);
                //发送有序的广播
                sendOrderedBroadcast(intent,null);
                break;
        }
    }
}

接收广播:

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".MyBroadcast">
            <!--设置广播优先级:intent-filter标签下的android:priority=""-->
            <intent-filter android:priority="1000">
                <action android:name="com.xch"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
MyBroadcast:

package com.example.broadcastreceive;

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

/**
 * Created by 杨雄超 on 2016/8/31.
 */
public class MyBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String str=intent.getExtras().getString("key");
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
        //拦截掉比当前优先级别低的广播
        abortBroadcast();
    }
}

 

 

以上是关于Android学习总结——Broadcast的主要内容,如果未能解决你的问题,请参考以下文章

android Broadcast 总结

Android学习笔记-Broadcast01-详解广播机制

Android学习笔记-Broadcast01-详解广播机制

Android学习笔记-Broadcast01-详解广播机制

EventBus学习总结

Linux内核设计第二周学习总结 完成一个简单的时间片轮转多道程序内核代码