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)新的頁(yè)面
? ? ? ? //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)文章
Android PC投屏功能實(shí)現(xiàn)的示例代碼
本篇文章主要介紹了Android PC投屏功能實(shí)現(xiàn)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android 動(dòng)畫之AlphaAnimation應(yīng)用詳解
本節(jié)講解AlphaAnimation 動(dòng)畫,窗口的動(dòng)畫效果,淡入淡出什么的,有些游戲的歡迎動(dòng)畫,logo的淡入淡出效果就使用AlphaAnimation,具體的祥看本文,需要的朋友可以參考下2012-12-12
android 有阻尼下拉刷新列表的實(shí)現(xiàn)方法
下面小編就為大家分享一篇android 有阻尼下拉刷新列表的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧2018-01-01
Android實(shí)現(xiàn)商城購(gòu)物車功能的實(shí)例代碼
最近公司項(xiàng)目做商城模塊,需要實(shí)現(xiàn)購(gòu)物車功能,主要實(shí)現(xiàn)了單選、全選,金額合計(jì),商品刪除,商品數(shù)量加減等功能,這篇文章主要介紹了Android實(shí)現(xiàn)商城購(gòu)物車功能,需要的朋友可以參考下2019-06-06
Android中View的炸裂特效實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android中View的炸裂特效實(shí)現(xiàn)方法,涉及Android組件ExplosionField的相關(guān)定義與使用技巧,需要的朋友可以參考下2016-07-07

