網站首頁 編程語言 正文
本文實例為大家分享了Android使用廣播發送消息的具體代碼,供大家參考,具體內容如下
1.activity_main.xml?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? <EditText ? ? ? ? android:id="@+id/edit" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:hint="請輸入要發送的內容" /> ? ? <Button ? ? ? ? android:id="@+id/send" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="發送"/> </LinearLayout>
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);
?
? ? ? ? //輸入框id
? ? ? ? edit = (EditText) findViewById(R.id.edit);
? ? ? ? //按鈕id
? ? ? ? send = (Button) findViewById(R.id.send);
?
? ? ? ? //點擊按鈕發送廣播
? ? ? ? send.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
?
? ? ? ? ? ? ? ? //獲取輸入的文本
? ? ? ? ? ? ? ? String content=edit.getText().toString();
?
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? //包名
? ? ? ? ? ? ? ? intent.setAction("xx.xx.xx");
? ? ? ? ? ? ? ? intent.putExtra("msg", content);
? ? ? ? ? ? ? ? sendBroadcast(intent);
?
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
3.創建廣播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) {
?
? ? ? ? //跳轉新的頁面
? ? ? ? //Intent i = new Intent(context, MainActivity2.class);
? ? ? ? //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? //context.startActivity(i);
?
? ? ? ? //獲取廣播內容
? ? ? ? String content=intent.getStringExtra("msg");
?
? ? ? ? Toast.makeText(context, "廣播接受者:"+content, Toast.LENGTH_SHORT).show();
? ? }
}
清單文件中記得注冊廣播
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? package="com.xxx.xxx"> ? ? ? <uses-sdk ? ? ? ? android:minSdkVersion="8" ? ? ? ? android:targetSdkVersion="18" ? ? ? ? tools:ignore="GradleOverrides,OldTargetApi" /> ? ? ? <application ? ? ? ? ? ... ? ? ? ? ? ? ? ? ? /> ? ? ? ? ? <receiver android:name=".MyReceiver" ? ? ? ? ? ? android:permission="TODO" ? ? ? ? ? ? tools:ignore="ExportedReceiver"> ? ? ? ? ? ? <intent-filter > ? ? ? ? ? ? ? ? <action android:name="com.xxx.xxx"/> ? ? ? ? ? ? </intent-filter> ? ? ? ? </receiver> ? ? ? ? ?... ? ? ? </application> </manifest>
原文鏈接:https://blog.csdn.net/weixin_42630638/article/details/111283289
相關推薦
- 2023-11-14 Kubernetes常用命令(持續更新)
- 2022-02-12 安卓給文件賦777讀寫權限
- 2024-02-25 前端顯示的日期時間與數據庫日期時間不一致
- 2022-05-05 Python&Matlab實現螞蟻群算法求解最短路徑問題的示例_python
- 2022-12-30 解決React報錯Style?prop?value?must?be?an?object_React
- 2023-07-26 TypeScript中的對象類型(可選屬性 只讀屬性 交叉類型)
- 2022-10-21 tomcat8中startup可以啟動tomcat8w無法啟動的問題分析_Tomcat
- 2022-04-25 Pytorch技法之繼承Subset類完成自定義數據拆分_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支