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); }//在頁面銷毀時(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. jsp文件下載功能實(shí)現(xiàn)代碼2. JSP實(shí)現(xiàn)帶查詢條件的通用分頁組件3. 一個(gè)用xslt樣式將xml解析為xhtml的類TransformBinder(兼容FF和IE7.0)4. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟5. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案6. 爬取今日頭條Ajax請(qǐng)求7. WML學(xué)習(xí)之三 顯示文本8. .NET Core Web APi類庫(kù)內(nèi)嵌運(yùn)行的方法9. Jquery使用原生AJAX方法請(qǐng)求數(shù)據(jù)10. C#抽象類的用法介紹
