日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android使用廣播發送消息_Android

作者:抱著回憶旅行 ? 更新時間: 2022-06-18 編程語言

本文實例為大家分享了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

欄目分類
最近更新