Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Android use broadcast to send messages

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how Android uses broadcast to send messages". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how Android uses broadcast to send messages" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

The specific effects are as follows

1.activity_main.xml

2.MainActivity

Import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity {private EditText edit; private Button send; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / input box id edit = (EditText) findViewById (R.id.edit) / / Button id send = (Button) findViewById (R.id.send); / / Click the button to send broadcast send.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / get the entered text String content=edit.getText (). ToString (); Intent intent = new Intent () / / package name intent.setAction ("xx.xx.xx"); intent.putExtra ("msg", content); sendBroadcast (intent);}});}}

3. Create a broadcast MyReceiver

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 {@ Override public void onReceive (Context context, Intent intent) {/ / Jump to the new page / / Intent I = new Intent (context, MainActivity2.class); / / i.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); / / context.startActivity (I) / / get broadcast content String content=intent.getStringExtra ("msg"); Toast.makeText (context, "broadcast recipient:" + content, Toast.LENGTH_SHORT). Show ();}}

Remember to register the broadcast in the manifest file

... After reading this, the article "how to use Android to broadcast and send messages" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report