• 旺仔:有序广播


    package com.example.administrator.test.Fragment.Broadcast;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    import com.example.administrator.test.R;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
        }
        public void bt1_OnClick(View v)
        {
            //一般广播
            Intent intent=new Intent( "Broadcast.MainActivity" );
            intent.putExtra( "name","广播发送测试" );
            sendBroadcast( intent );
            Toast.makeText( this, "一般广播已经发出去了", Toast.LENGTH_SHORT ).show();
        }
        //动态注册
        MyReceiver mr;
        public void bt2_OnClick(View v)
        {
            mr=new MyReceiver();
            IntentFilter interFilter=new IntentFilter( "Broadcast.MainActivity1" );
           // interFilter.setPriority( 1000 );
            registerReceiver( mr,interFilter );
    
        }
    
        //发送有序广播
        public void bt3_OnClick(View v)
        {
            Intent intent=new Intent( "Broadcast.MainActivity" );
            intent.putExtra( "name","广播发送测试" );
            sendOrderedBroadcast( intent ,null);//第二个参数叫权限字符串,一般设为null
            Toast.makeText( this, "有序广播已经发出去了", Toast.LENGTH_SHORT ).show();
        }
        //反注册
        @Override
        protected void onDestroy() {
            if (mr!=null)
            {
                unregisterReceiver( mr );
            }
            super.onDestroy();
        }
    }
    package com.example.administrator.test.Fragment.Broadcast;
    
            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;
            import android.util.Log;
            import android.widget.Toast;
    
    public class MyReceiver extends BroadcastReceiver {
        public MyReceiver() {
            Log.e( "TAG","MyReceiver被实例化" );
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //阻断有序广播接收
            if (isOrderedBroadcast())
            {
               // abortBroadcast();
            }
            String str=intent.getStringExtra( "name" );
            intent.putExtra( "name1","这是修改的内容" );
            Toast.makeText( context, "MyReceiver收到的广播="+str, Toast.LENGTH_SHORT ).show();
        }
    }
    package com.example.administrator.test.Fragment.Broadcast;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MyReceiver2 extends BroadcastReceiver {
        public MyReceiver2() {
            Log.e("TAG","MyReceiver被实例化");
    
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String str=intent.getStringExtra( "name1" );
            Toast.makeText( context, "MyReceive2收到的广播="+str, Toast.LENGTH_SHORT ).show();
        }
    }
     <activity android:name=".Fragment.Broadcast.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
    
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <receiver
                android:name=".Fragment.Broadcast.MyReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="100">
                    <action android:name="Broadcast.MainActivity"/>
                </intent-filter>
            </receiver>
            <receiver
                android:name=".Fragment.Broadcast.MyReceiver2"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="100">
                    <action android:name="Broadcast.MainActivity"/>
                </intent-filter>
            </receiver>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送一般广播"
            android:onClick="bt1_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="动态注册广播接收器"
            android:onClick="bt2_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送有序广播"
            android:onClick="bt3_OnClick"/>
    </LinearLayout>
  • 相关阅读:
    正则
    在开发过程中调试报表插件详细教程
    在开发过程中调试报表插件详细教程
    页面导出Excel文件总结
    java.lang.IllegalArgumentException: sheetName '' is invalid
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
    表达式中的一些常用模式.
    C++使用libcurl做HttpClient
    C++ curl跨平台HttpClient
    java.lang.NumberFormatException: empty String
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5730074.html
Copyright © 2020-2023  润新知