码迷,mamicode.com
首页 > 移动开发 > 详细

android BroadcastReceiver

时间:2016-04-25 09:11:38      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

AbdroidManifast.xml

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

    <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=".MyReceiver2"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="10">
                <action android:name="com.hanqi.textbr.action"/>
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="20">
                <action android:name="com.hanqi.textbr.action"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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: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.hanqi.testbr.MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="发送一般广播"
        android:onClick="bt1_onclick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="发送有序广播"
        android:onClick="bt2_onclick"/>

</LinearLayout>

MainActivity.java

package com.hanqi.testbr;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void bt1_click(View v)
    {
        //发送一般广播
        //1.准备意图Intent
        Intent intent = new Intent("com.hanqi.textbr.action");

        intent.putExtra("data","广播发出了");
        //2.发送
        sendBroadcast(intent);

        Toast.makeText(MainActivity.this, "我发送了广播", Toast.LENGTH_SHORT).show();
    }
    public void bt2_click(View v)
    {
        //发送有序广播
        //1.准备意图Intent
        Intent intent = new Intent("com.hanqi.textbr.action");

        intent.putExtra("data","有序广播发出了");
        //2.发送
        sendOrderedBroadcast(intent,null);

        Toast.makeText(MainActivity.this, "我发送了有序广播", Toast.LENGTH_SHORT).show();
    }
}

MyReceiver.java

package com.hanqi.testbr;

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

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
        Log.e("ATG","构造广播接收器");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String str = intent.getStringExtra("data");

        // 处理广播
        Log.e("ATG","收到广播了 = "+str);

        if (isOrderedBroadcast())
        {
            abortBroadcast();
            Log.e("ATG", "我阻断了有序广播");
        }

    }
}

MainActivity2.java

package com.hanqi.testbr;

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

public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
        Log.e("ATG","构造广播接收器2");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String str = intent.getStringExtra("data");

        // 处理广播
        Log.e("ATG","收到广播了2 = "+str);

    }
}

 

android BroadcastReceiver

标签:

原文地址:http://www.cnblogs.com/cuikang/p/5429643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!