Android static registration and dynamic registration and broadcast data on the broadcast write their feelings and experiences interface

Android static registration and dynamic registration and broadcast data on the broadcast write their feelings and experiences interface

I remember when I was in school, each class in the classroom will be equipped with a speaker, these speakers are the access to the school's radio room, once notified of what is important, it will play a broadcast to inform school teachers and students . Andrews also broadcast we usually use a similar mechanism to let the program Andrews obtain status information system, a built-in system-level broadcast, but often can not meet the demand, so the custom broadcast on the use of very extensive. Here are two ways to customize talk radio

Static state

Registration is still to be registered directly in AndroidManifest inside, add receiter, which need to write the name of your Broadcast Receiver, I am here MyReceiver, you can also write other names, enabled and exported are set to true; these are generated by default, according to their needs change, they have to manually generated is intent-filter, add the action, written action name (the name of what you own and what the definition is the name of the company are used to distinguish com.xxx.cn) , add permissions uses-permission

Here Insert Picture Description
Here Insert Picture Description

dynamic

Dynamic registration broadcast in the event, I have here the first layout created a new button in the activities initialization own radio, set the action name, and then set the button click event, the use of Intent intent = new Intent (); set the action name is non-standard I set up here, I'll name a few random, then sendBroadcast (intent); transmitting broadcast, two things: 1, the broadcast should set global variables private myReceiver myReceiver; 2, dynamic registration the broadcast use onDestroy be closed
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

After the experiences of the dynamic and static use

Dynamic more flexible, open and close freely registered grasp at the event, static is relatively straightforward, it is registered in the Manifest inside, and then where to use direct action statement can be used, but Android 8.0 after the most static registration was canceled, which is a little pit, it seems still want the flexibility to use the code, to use dynamic registration

About dynamic registration broadcast and data broadcast recorded in the interface

On the radio:

Dynamic registration according to the above was added, as an agent to add an interface to I here demonstrated the direct pinyin abbreviation for the DL profile, in order to write the data so that the agent is added in the inside of the method does not return a value void xr (String s); write to take the data after their arrival to the data transfer interface, a write method of the agent pass into the transfer, the last written data dl.xr (s) to be transmitted;

Here Insert Picture Description

At the event:

To add the first implementation of the interface implements MyReceiver.DL broadcast, since it is to record the data, it should record it, I add here where in the layout file a TextView, and set the global variable in private activities, then implement the transfer to this interface method myReceiver.CS (this); and finally writes the data in TextView

     public class MainActivity extends AppCompatActivity implements MyReceiver.DL{
private MyReceiver myReceiver;
private  TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver=new MyReceiver();
    IntentFilter intentFilter=new IntentFilter("CCTV_1");
    registerReceiver(myReceiver,intentFilter);

    myReceiver.CS(this);
   tv=findViewById(R.id.textView);


    Button button=findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("CCTV_1");
            intent.putExtra("s","呆呆兽");
            sendBroadcast(intent);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(myReceiver);
}
@Override
public void xr(String s) {
    tv.setText(s);
}
}

Many script intercepts data during transmission of data, but now there are many anti-script to prevent malicious data theft.
The first hair blog, just learning android technology white, deep understanding, badly written, there will be a lot of mistakes, a lot needs to learn from the older generation, sincerely hope bigwigs let me know, thanks.
Would like to thank my teacher Jin Tingbo, lectures easy to understand, patiently taught us, let me do something in terms of man are harvested. https://me.csdn.net/jintingbo

Published an original article · won praise 0 · Views 110

Guess you like

Origin blog.csdn.net/zc_0107/article/details/105267643