欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用廣播發(fā)送消息

 更新時(shí)間:2022年04月19日 11:41:39   作者:抱著回憶旅行  
這篇文章主要為大家詳細(xì)介紹了Android使用廣播發(fā)送消息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論