BroadcastReceiver(广播)的静态注册和动态注册 --Android开发

BroadcastReceiver是安卓四大组件之一,本例通过代码的方式演示静态注册和动态注册。

1、静态注册

静态注册只需要AndroidManifest.xml中进行配置:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="thonlon.example.cn.ipdaildemo">
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<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=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>

下面是相关的代码:

MyBroadcastReceiver.java:

package thonlon.example.cn.ipdaildemo;

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

/**
* Created by NIUXINLONG on 2018/6/21.
*/
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
setResultData(number+"123456");
}
}

MainActivity.java:

package thonlon.example.cn.ipdaildemo;

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

public class MainActivity extends AppCompatActivity {

private MyBroadcastReceiver myBroadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();//在主活动中调用了广播使广播生效
}

2、动态注册

需要在Java代码中进行如下设定:

MainActivity.java:

package thonlon.example.cn.ipdaildemo;

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

public class MainActivity extends AppCompatActivity {

private MyBroadcastReceiver myBroadcastReceiver;

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

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
myBroadcastReceiver = new MyBroadcastReceiver();
registerReceiver(myBroadcastReceiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);//销毁广播
}
}
另外,动态相互侧虽然不需要在AndroidManifest.xml中配置<receiver>,但是不要忘了在配置文件加上广播类型的权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

转载于:https://www.cnblogs.com/qikeyishu/p/9210959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值