BroadCastReceiver 安卓广播接收者

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

    <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">
        <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="com.feng.broad"></action>
            </intent-filter>

        </receiver>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.feng.broad"></action>
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>





public class MainActivity extends AppCompatActivity {
    
    

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

        sendOrderId = findViewById(R.id.send_order_id);
        sendOrderId.setOnClickListener(this);
        sendId = findViewById(R.id.send_id);
        sendId.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
    
    
        int id = v.getId();
        switch (id) {
    
    
            case R.id.send_id:
                Intent intent = new Intent();
                intent.setAction("com.feng.broad");
                Bundle bundle = new Bundle();
                bundle.putInt("msg",123);
                intent.putExtras(bundle);
                sendBroadcast(intent);
                break;

            case R.id.send_order_id:
                Intent intent1 = new Intent();
                intent1.setAction("com.feng.broad");
                sendOrderedBroadcast(intent1,null);
                break;

            default:
                break;
        }
    }

    @Override
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    }
}





public class MyReceiver extends BroadcastReceiver {
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        private static final String TAG = "MyReceiver";
        @Override
        public void onReceive(Context context, Intent intent) {
    
    
            String action = intent.getAction();
            if(BroadcastConst.ACTION.equals(action)){
    
    
                Log.i(TAG, "onReceive: ");
            }
        }
    }
}





public class MyReceiver2 extends BroadcastReceiver {
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
        String action = intent.getAction();
        if(action.equals("com.feng.broad")){
    
    

            Log.i(TAG, "onReceive: +++");
            if(isOrderedBroadcast()){
    
    
                abortBroadcast();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46367373/article/details/104663314