日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 跨应用发送自定义广播

發(fā)布時間:2024/1/1 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 跨应用发送自定义广播 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

話不多說,直接看效果圖和代碼!

一、效果圖

1、未發(fā)送廣播之前,兩個APP的主界面圖;


2、發(fā)送之后,文本框內(nèi)容改變。

二、代碼

1、創(chuàng)建第一個APP
(1)MainActivity中代碼如下:

package com.example.study;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.TextView;public class MainActivity extends AppCompatActivity {EatReceiver eatReceiver;@SuppressLint("StaticFieldLeak")static TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//文本框textView = findViewById(R.id.textview);}public static class EatReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {textView.setText("蕪湖");}} }

注意這里的廣播接收器前面的修飾符必須是:public static。

(2)靜態(tài)注冊“廣播接收器”

<receiver android:name="com.example.study.MainActivity$EatReceiver"tools:ignore="Instantiatable"android:exported="true"><intent-filter><action android:name="android.intent.action.WuHu"/></intent-filter></receiver>

這里需要注意的是:
(1)接收器的名字格式是:“包名.類名$內(nèi)部類名”;
(2)android:exported=“true”:可以接收其他應(yīng)用程序發(fā)送的廣播;

(3)或者動態(tài)注冊”廣播接收器“

package com.example.study;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.TextView;public class MainActivity extends AppCompatActivity {EatReceiver eatReceiver;@SuppressLint("StaticFieldLeak")static TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//動態(tài)注冊廣播接收器IntentFilter intentFilter = new IntentFilter("android.intent.action.WuHu");EatReceiver eatReceiver = new EatReceiver();registerReceiver(eatReceiver,intentFilter);//文本框textView = findViewById(R.id.textview);}public static class EatReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {textView.setText("蕪湖");}} }

2、創(chuàng)建第二個APP
(1)MainActivity中代碼如下:

package com.example.study2;import androidx.appcompat.app.AppCompatActivity;import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent("android.intent.action.WuHu"); // ComponentName componentName = new ComponentName("com.example.study","com.example.study.MainActivity$EatReceiver"); // intent.setComponent(componentName);intent.setPackage("com.example.study");sendBroadcast(intent);}});} }

這里需要注意的是: 給intent指定廣播接收器的位置(兩種方式):
(1)intent.setPackage():設(shè)置廣播接收器所在的包名;
(2)new ComponentName(“包名”,“包名.類名”)
?intent.setComponent();
?設(shè)置廣播接收器的包名和類名。

總結(jié)

以上是生活随笔為你收集整理的Android 跨应用发送自定义广播的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。