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

android入门——BroadCast(2)

时间:2016-09-22 06:28:31      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

自定义广播

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wkp.broadcast">

    <!-- 声明自定义权力 -->
    <permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/>
    <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>

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyReceiver2">
            <intent-filter>
                <action android:name="com.example.wkp.broadcast.MY_ACTION"/>
            </intent-filter>
        </receiver>
         <!--静态注册 -->
        <receiver android:name=".MyReceiver">
            <intent-filter>
                <!-- 动作   打开飞行模式时触发广播 -->
                <!--<action android:name="android.intent.action.AIRPLANE_MODE"/>-->
                <!-- 收到短信时  暂时无法实现 -->
                <!--<action android:name="android.provider.Telephony.SMS_RECEIVED"/>-->
                <!--<action android:name="android.provider.Telephony.SMS_DELIVER"/>-->
            </intent-filter>
        </receiver>
    </application>

    <!-- 开启短信权限 -->
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

    <!-- 自定义权力 -->
    <uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"></uses-permission>

</manifest>
AndroidMainfest.xml

注意声明自定义权力 

<permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/>

开启权力

<uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"></uses-permission>

静态注册

<receiver android:name=".MyReceiver2">
<intent-filter>
<action android:name="com.example.wkp.broadcast.MY_ACTION"/>
</intent-filter>
</receiver>
技术分享
package com.example.wkp.broadcast;

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

/**
 * Created by wkp on 2016/9/21.
 */
public class MyReceiver2 extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("hh","get broadcast");
    }
}
MyReceiver2.java

接收器

技术分享
package com.example.wkp.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 * Created by wkp on 2016/9/21.
 */
public class SecondActivity extends Activity {
    private Button btn=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btn= (Button) findViewById(R.id.send);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("com.example.wkp.broadcast.MY_ACTION");
                sendBroadcast(intent,"com.example.wkp.broadcast.MY_PEMISSION");
                Log.v("hehe","already send");
            }
        });
    }
}
SecondActivity.java

点击按钮发送广播

 

 

有序广播先定义先接收

abortBroadcast();忽略广播  

可以通过android:priority制定优先级  不写为0

可以设置是否接收其他app的广播

可以设置是否被其他app接收广播 本地广播LocalBroadcastManager

private LocalBroadcastManager manager= LocalBroadcastManager.getInstance(this);

LocalReceiver receiver=new LocalReceiver();

IntentFilter filter=new IntentFilter();

filter.addAction("com.example.wkp.broadcast.MY_ACTION");

manager.registerReceiver(receiver,filter);

 

android入门——BroadCast(2)

标签:

原文地址:http://www.cnblogs.com/wangkaipeng/p/5894741.html

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