android几个自定义广播和系统广播的用法

1.无序广播和有序广播

    1.首先定义一个写发送内容的控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_height="match_parent" tools:context="com.example.android_sender.MainActivity">

   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="请输入要发送的广播:"
       android:id="@+id/rs_EditText"
       />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送"
        android:onClick="send"
        />
</LinearLayout>
    2.首先在java类里拿到要发送的内容,给这个广播定义一个名字加入Intent,选择发送类型即可。

     //无序广播
    sendBroadcast(intent);//发送无序广播

     可以把无序的替换成有序的,有序的可以设置接收广播的先后顺序
     intentFilter.setPriority(-100); 取值在-1000到1000大的先接收广播

    //有序广播
    //sendOrderedBroadcast(intent,null);//发送有序广播
package com.example.android_sender;

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

public class MainActivity extends AppCompatActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.rs_EditText);
    }
    public void send(View view){
       String content= editText.getText().toString();
        Intent intent=new Intent();
        //广播内容
        intent.putExtra("content",content);
        //广播名字
        intent.setAction("com.example.android_sender_Hug");
        //无序广播
        sendBroadcast(intent);//发送无序广播
    }
}
 3.接收广播写一个类继承 BroadcastReceiver 实现一个onCreate方法。
    接收广播有两种类型:一种是静态,另一种是动态,以下的是动态接收
package com.example.android_sender_received01;

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

public class MainActivity extends AppCompatActivity {

    private Myreceived01 myreceived01;
    private IntentFilter intentFilter;

    //动态接收
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       myreceived01 = new Myreceived01();//自己写的类
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.android_sender_Hug");
        intentFilter.setPriority(-100);//设置先后顺序
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注册广播
        registerReceiver(myreceived01,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁
        unregisterReceiver(myreceived01);
    }
}
package com.example.android_sender_received01;

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

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class Myreceived01 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.example.android_sender_Hug".equals(intent.getAction())){
            //拿到广播内容
            String Scontent=  intent.getStringExtra("content");
            Log.i("text","1     "+Scontent.toString());
        }
    }
}

3.1以下的是静态接收

package com.example.android_sender_received02;

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

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class Myreceived02 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.example.android_sender_Hug".equals(intent.getAction())){
            String Scontent=intent.getStringExtra("content");
            Log.i("text","2     "+Scontent);
        }
    }
}




注意配置xml (动态不用配置)
  <receiver android:name=".Myreceived02">
    <intent-filter
        android:priority="1000"//设置先后顺序
        >//获取广播的名字
        <action android:name="com.example.android_sender_Hug"></action>
    </intent-filter>
</receiver>

2.监控接收来电和接受短信

1.接受来电
package com.example.android_phone;

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

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class Myphone extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {//打电话

            if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
                TelephonyManager thm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
                //拿到对方的手机号
            String phone=intent.getStringExtra("incoming_number");
                //获取当前状态
             int state=thm.getCallState();
                switch (state){
                    case TelephonyManager.CALL_STATE_RINGING://响铃 0
                        Log.i("text","响铃 "+phone);
                        break;
                    case TelephonyManager.CALL_STATE_IDLE://通话结束  2
                        Log.i("text","通话结束 "+phone);
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK://接听  1
                        Log.i("text","接听 "+phone);
                        break;
                }
            }
    }

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

    <!--读取电话状态的权限-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" 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>
        <receiver android:name=".Myphone">
            <intent-filter><action android:name="android.intent.action.PHONE_STATE"></action></intent-filter>
        </receiver>
    </application>

</manifest>

2.接收短信

package com.example.android_sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

/**
 * Created by Administrator on 2017/7/13 0013.
 */

public class Mymsm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
            Bundle bundle=intent.getExtras();
            Object object[] = (Object[]) bundle.get("pdus");//拿到短信 
            SmsMessage sms[]=new SmsMessage[object.length];//处理短信的类
            for (int i = 0; i < object.length; i++) {
                sms[i]= SmsMessage.createFromPdu((byte[]) object[i]);//把拿到的短信内容加入处理短信的类
            }
            //遍历信息
            for (SmsMessage sm : sms) {
               String phone=sm.getDisplayMessageBody();//获取发送人
               String content= sm.getDisplayOriginatingAddress();//获取短信内容
                Log.i("text",phone+":="+content);
            }
        }
    }
}

配置Xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_sms">
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" 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>
        <receiver android:name=".Mymsm">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值