我是靠谱客的博主 无心手套,这篇文章主要介绍Android自定义广播接收,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Android自定义广播接收的具体代码,供大家参考,具体内容如下

实现效果:

MainActivity.java代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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) { //动态注册广播接收 receiver = new MyBroadcastReceiver(); String action = "henurjxy"; IntentFilter intentFilter=new IntentFilter(action); registerReceiver(receiver,intentFilter); Intent intend = new Intent("henurjxy"); sendBroadcast(intend); } //在页面销毁时,注销广播接收者 @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } }

MyBroadcastReceiver.java代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,"是我发出的广播哦",Toast.LENGTH_SHORT).show(); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是无心手套最近收集整理的关于Android自定义广播接收的全部内容,更多相关Android自定义广播接收内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(148)

评论列表共有 0 条评论

立即
投稿
返回
顶部