In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use local broadcasting in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Android article on how to use local radio. Let's take a look.
MainActivity code
Package com.example.luobo.mybroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.support.v4.content.LocalBroadcastManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button button; private IntentFilter intentFilter; private LocalBroadcastManager localBroadcastManager; private LocalReceiver localReciiver @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); button = (Button) findViewById (R.id.send_button); button.setOnClickListener (this); localBroadcastManager = LocalBroadcastManager.getInstance (this); / / use intentFilter = new IntentFilter (); intentFilter.addAction ("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"); localReciiver = new LocalReceiver (); localBroadcastManager.registerReceiver (localReciiver,intentFilter) } @ Override protected void onDestroy () {super.onDestroy (); localBroadcastManager.unregisterReceiver (localReciiver);} @ Override public void onClick (View view) {Intent intent = new Intent ("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"); localBroadcastManager.sendBroadcast (intent);} class LocalReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {Toast.makeText (context, "received local broadcast", Toast.LENGTH_SHORT). Show ();}
First of all, the instance is obtained through the getInstance (this) method of LocalBroadcastManager (local broadcast management class). When registering the broadcast message, the registerReceiver (parameter 1, parameter 2) method of the localBroadcastManager instance is called (parameter 1 is the local broadcast receiver, parameter 2 is that the filter only selects to receive the specific broadcast message), and the sendBroadcast (Initent initent) method of the localBroadcastManager instance is called to send the broadcast message.
MyRecevity
Package com.example.luobo.mybroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {Toast.makeText (context, "Received in MyBroadCastReceiver", Toast.LENGTH_SHORT) .show (); abortBroadcast ();}}
Activity_main.xml
AndroidMainfest.aml
This is the end of the article on "how to use Local Broadcasting in Android". Thank you for reading! I believe you all have a certain understanding of "how to use local broadcasting in Android". If you want to learn more, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.