Android Broadcast Receiver-android learning tour

Source: Internet
Author: User

Android Broadcast Receiver-android learning tour
First, inherit the BroadcastReceiver class and register it in manifest.

public class MyReceiver extends BroadcastReceiver {    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        throw new UnsupportedOperationException(Not yet implemented);    }}

Register in mainifest


          
  
Dynamically register and cancel broadcast receivers

Code:
Worker Er:

public class MyReceiver extends BroadcastReceiver {    public static final String ACTION = peng.liu.testview.intent.action.MyReceiver;    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        System.out.println(intent.getStringExtra(data)+hello);    }}

Main class:

public class MainActivity extends Activity implements View.OnClickListener{    private MyReceiver receiver = null;    private Button send,reg,unReg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.send).setOnClickListener(this);        findViewById(R.id.reg).setOnClickListener(this);        findViewById(R.id.unReg).setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.send:                Intent intent = new Intent(MyReceiver.ACTION);                intent.putExtra(data,jiekxueyuan);                sendBroadcast(intent);                break;            case R.id.reg:                if (receiver == null){                    receiver = new MyReceiver();                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));                }                break;            case R.id.unReg:                if (receiver != null){                    unregisterReceiver(receiver);                    receiver = null;                }                break;        }    }}

Layout code:


  
   
   
  
Broadcast priority

Static registration in manifest this time


           
  

Android: priority: Used to set priority. A larger number indicates a higher priority.

High-priority terminal Broadcast
// Note that sendOrderedBroadcast (intent, null); Intent intent = new Intent (MyReceiver. ACTION); intent. putExtra (data, jiekxueyuan); sendOrderedBroadcast (intent, null );

Receiving part

Public class MyReceiver2 extends BroadcastReceiver {public MyReceiver2 () {}@ Override public void onReceive (Context context, Intent intent) {System. out. println (intent. getStringExtra (data); // This user is used to interrupt the next low-priority acceptance of abortBroadcast ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.