Android自定義廣播接收
本文實(shí)例為大家分享了Android自定義廣播接收的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果:
MainActivity.java代碼:
package com.henu.broadcastsample;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private MyBroadcastReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); } public void send(View v) {//動(dòng)態(tài)注冊(cè)廣播接收receiver = new MyBroadcastReceiver();String action = 'henurjxy';IntentFilter intentFilter=new IntentFilter(action);registerReceiver(receiver,intentFilter);Intent intend = new Intent('henurjxy');sendBroadcast(intend); }//在頁(yè)面銷毀時(shí),注銷廣播接收者 @Override protected void onDestroy() {super.onDestroy();unregisterReceiver(receiver); }}
MyBroadcastReceiver.java代碼:
package com.henu.broadcastsample;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;public class MyBroadcastReceiver extends BroadcastReceiver {public MyBroadcastReceiver(){} @Override public void onReceive(Context context, Intent intent) {Toast.makeText(context,'是我發(fā)出的廣播哦',Toast.LENGTH_SHORT).show(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)3. .NET6打包部署到Windows Service的全過程4. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問題5. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)6. Python編寫nmap掃描工具7. Ajax返回值類型與用法實(shí)例分析8. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法9. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼10. 如何在jsp界面中插入圖片
