import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DynamicReceiver dynamicReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化IntentFilter对象
IntentFilter filter = new IntentFilter();
filter.addAction("panhouye");
dynamicReceiver = new DynamicReceiver();
//注册广播接收
registerReceiver(dynamicReceiver,filter);
}
//按钮点击事件
public void send2(View v){
Intent intent = new Intent();
intent.setAction("panhouye");
intent.putExtra("sele","潘侯爷");
sendBroadcast(intent);
}
/*动态注册需在Acticity生命周期onPause通过
*unregisterReceiver()方法移除广播接收器,
* 优化内存空间,避免内存溢出
*/
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(new MyReceiver());
}
//通过继承 BroadcastReceiver建立动态广播接收器
class DynamicReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//通过土司验证接收到广播
Toast t = Toast.makeText(context,"动态广播:"+ intent.getStringExtra("sele"), Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);//方便录屏,将土司设置在屏幕顶端
t.show();
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast t = Toast.makeText(context,"静态广播:"+intent.getStringExtra("info"), Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);
t.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.day19">
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<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配置文件
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
//自定义Action
<action android:name="MLY" />
</intent-filter>
</receiver>
</application>
</manifest>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DynamicReceiver dynamicReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//静态广播点击
public void send(View v){
Intent intent = new Intent();
intent.setAction("MLY");
intent.putExtra("info","panhouye");
sendBroadcast(intent);
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DynamicReceiver dynamicReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("panhouye");
dynamicReceiver = new DynamicReceiver();
registerReceiver(dynamicReceiver,filter);
}
//静态广播点击
public void send(View v){
Intent intent = new Intent();
//设置与动态相同的Action,方便同时触发静态与动态
intent.setAction("panhouye");
intent.putExtra("info","潘侯爷");
sendBroadcast(intent);//默认广播
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(new MyReceiver());
}
class DynamicReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast t = Toast.makeText(context,"动态广播:"+ intent.getStringExtra("info"), Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);
t.show();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("panhouye");
filter.setPriority(-1000);//设置动态优先级
dynamicReceiver = new DynamicReceiver();
registerReceiver(dynamicReceiver,filter);
}
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
//设置静态优先级
<intent-filter android:priority="1000">
<action android:name="panhouye" />
</intent-filter>
</receiver>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.test19">
//添加拨打电话权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<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=".StaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
//设置打电话对应的action
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
DynamicReceiver dynamicReceiver;//声明动态注册广播接收
@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");
dynamicReceiver = new DynamicReceiver();
registerReceiver(dynamicReceiver,filter);
Log.i("Tag","Activity-onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i("Tag","Activity-onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i("Tag","Activity-onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i("Tag","Activity-onPause");
}
@Override
protected void onStop() {
super.onPause();
Log.i("Tag","Activity-onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("Tag","Activity-onDestroy");
unregisterReceiver(dynamicReceiver);
}
class DynamicReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Tag","动态注册广播接收到您正在拨打电话"+getResultData());
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class StaticReceiver extends BroadcastReceiver {
public StaticReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Tag","静态注册广播接收到您正在拨打电话"+getResultData());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有