Android使用廣播發(fā)送消息
本文實(shí)例為大家分享了Android使用廣播發(fā)送消息的具體代碼,供大家參考,具體內(nèi)容如下
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="請(qǐng)輸入要發(fā)送的內(nèi)容" /> ? ? <Button ? ? ? ? android:id="@+id/send" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="發(fā)送"/> </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); ? ? ? ? ? //點(diǎn)擊按鈕發(fā)送廣播 ? ? ? ? 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.創(chuàng)建廣播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) { ? ? ? ? ? //跳轉(zhuǎn)新的頁面 ? ? ? ? //Intent i = new Intent(context, MainActivity2.class); ? ? ? ? //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ? ? ? ? //context.startActivity(i); ? ? ? ? ? //獲取廣播內(nèi)容 ? ? ? ? String content=intent.getStringExtra("msg"); ? ? ? ? ? Toast.makeText(context, "廣播接受者:"+content, Toast.LENGTH_SHORT).show(); ? ? } }
清單文件中記得注冊(cè)廣播
<?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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例探究Android開發(fā)中Fragment狀態(tài)的保存與恢復(fù)方法
這篇文章主要介紹了實(shí)例探究Android開發(fā)中Fragment狀態(tài)的保存與恢復(fù)方法,或許開發(fā)者們對(duì)Fragment的操作都比較熟悉,但onSaveInstanceState()方法并不能夠很好地保存Fragment狀態(tài),需要的朋友可以參考下2016-04-04Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android5.0中多種水波紋效果的實(shí)現(xiàn)代碼,水波紋效果大致上可以分為兩種,一種是有界的,一種無界,一起跟隨小編過來看看吧2018-05-05android用java和c實(shí)現(xiàn)查找sd卡掛載路徑(sd卡路徑)的方法
這篇文章主要介紹了android用java和c實(shí)現(xiàn)查找sd卡掛載路徑(sd卡路徑)的方法,需要的朋友可以參考下2014-02-02Android App中ViewPager與Fragment結(jié)合的一些問題解決
這篇文章主要介紹了Android App中ViewPager與Fragment結(jié)合的一些問題解決,重點(diǎn)講解了如何更新及替換ViewPager中的Fragment,需要的朋友可以參考下2016-03-03Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果,實(shí)現(xiàn)精彩的跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android Wear計(jì)時(shí)器開發(fā)
這篇文章主要介紹了Android Wear計(jì)時(shí)器開發(fā),需要的朋友可以參考下2014-11-11Android 自定義控件實(shí)現(xiàn)顯示文字的功能
這篇文章主要介紹了Android 自定義控件實(shí)現(xiàn)顯示文字的功能的相關(guān)資料,需要的朋友可以參考下2016-11-11