• 自定义的BroadCastReceiver


    1.MainActivity2.java中的代码,主要是使用意图发送广播

    public class MainActivity2 extends Activity{
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.receiver_demo);
    	};
    		
    	
    	public void click(View view){
    		Intent intent=new Intent();
    		intent.setAction("com.example.brocast.nimei");
    		sendBroadcast(intent);
    		
    		
    	}
    

    2.在Manifest.xml过滤意图,然后设置BrocastReceiver所在的类文件

      <receiver android:name="com.example.brocastreceiverdemo.BrocastReceiverDemo2" >
                <intent-filter>
                    <action android:name="com.example.brocast.nimei" />
                </intent-filter>
            </receiver>
    

     3、receiver.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="发送自定义广播" />
    
    </LinearLayout>
    

     4. 自定广播的代码

    public class BrocastReceiverDemo2 extends BroadcastReceiver{
    
    	@Override
    	public void onReceive(Context arg0, Intent arg1) {
    		// TODO Auto-generated method stub
    		Toast.makeText(arg0, "我是自定义的广播", 1).show();
    	}
    
    }
    
  • 相关阅读:
    Vue-router笔记
    webpack及其配置
    高阶函数+组件化开发
    Es6语法+v-on参数相关+vue虚拟dom
    英语资源及其APP推荐
    Qt获取ip地址
    QT创建和使用动态链接库
    error C2664: “strcmp”: 不能将参数 1 从“WCHAR [260]”转换为“const char *”
    Vue|VUE router 导航重复点击报错的问题解决方案
    Vue|触发路由跳转
  • 原文地址:https://www.cnblogs.com/childhooding/p/4334159.html
Copyright © 2020-2023  润新知