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

android studio廣播機制使用詳解

 更新時間:2022年08月11日 15:49:30   作者:冰山丶一角  
這篇文章主要為大家詳細介紹了android studio廣播機制的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Intent 是一種消息傳播機制,用于組件之間數(shù)據(jù)交換和發(fā)送廣播消息。通過本次實驗了解 Android 系統(tǒng)的組件通信原理,掌握利用 Intent 啟動其他組件的方法,以及利用 Intent 獲取信息和發(fā)送廣播消息的方法。

1、實現(xiàn)具有“登錄”按鈕的主界面,輸入用戶名、密碼,點擊登錄按鈕后,經(jīng)過判斷進入一個廣播Activity(需要傳遞主界面的用戶名)

2、在廣播Activity中,輸入信息,點擊發(fā)送廣播按鈕發(fā)送廣播,并且在廣播接收器中接收廣播并顯示。

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
? ? tools:context=".MainActivity">
? ? <GridLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:useDefaultMargins="true"
? ? ? ? android:columnCount="4">
? ? ? ? <TextView
? ? ? ? ? ? android:layout_columnSpan="1"
? ? ? ? ? ? android:layout_gravity="right"
? ? ? ? ? ? android:text="用戶名"/>
?
? ? ? ? <EditText
? ? ? ? ? ? ? ? android:ems="16"
? ? ? ? ? ? ? ? android:layout_columnSpan="3"
? ? ? ? ? ? ? ? android:id="@+id/user"/>
? ? ? ? <TextView
? ? ? ? ? ? android:layout_columnSpan="1"
? ? ? ? ? ? android:layout_gravity="right"
? ? ? ? ? ? android:layout_column="0"
? ? ? ? ? ? android:text="密碼"/>
?
? ? ? ? <EditText
? ? ? ? ? ? android:ems="16"
? ? ? ? ? ? android:layout_columnSpan="3"
? ? ? ? ? ? android:id="@+id/password"/>
? ? ? ? <Button
? ? ? ? ? ? android:text="登錄"
? ? ? ? ? ? android:id="@+id/signin"
? ? ? ? ? ? android:layout_column="1"
? ? ? ? ? ? android:layout_gravity="fill_horizontal"/>
? ? ? ? <Button
? ? ? ? ? ? android:text="退出"
? ? ? ? ? ? android:id="@+id/signout"
? ? ? ? ? ? android:layout_column="2"
? ? ? ? ? ? android:layout_gravity="fill_horizontal"/>
?
? ? </GridLayout>
?
?
</androidx.constraintlayout.widget.ConstraintLayout>

activity_my_brocast_reveicer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
? ? tools:context=".MySendBrocastReceiver">
? ? <GridLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:useDefaultMargins="true"
? ? ? ? android:columnCount="4">
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_columnSpan="1"
? ? ? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? android:layout_gravity="left"
? ? ? ? ? ? ? ? android:text="歡迎你"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:layout_width="match_parent"/>
? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? android:layout_gravity="left"
? ? ? ? ? ? ? ? android:id="@+id/name"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:layout_width="match_parent"/>
? ? ? ? </LinearLayout>
? ? ? ? <EditText
? ? ? ? ? ? android:ems="16"
? ? ? ? ? ? android:layout_column="0"
? ? ? ? ? ? android:layout_columnSpan="3"
? ? ? ? ? ? android:id="@+id/text"/>
? ? ? ? <Button
? ? ? ? ? ? android:text="發(fā)送廣播"
? ? ? ? ? ? android:id="@+id/send"
? ? ? ? ? ? android:layout_column="0"
? ? ? ? ? ? android:layout_gravity="fill_horizontal"/>
?
? ? </GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MyReceiver.java

package com.example.intendbrocastreceiver;
?
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
?
public class MyReceiver extends BroadcastReceiver {
?
? ? @Override
? ? public void onReceive(Context context, Intent intent) {
? ? ? ? // TODO: This method is called when the BroadcastReceiver is receiving
? ? ? ? // an Intent broadcast.
? ? ? ? String name=intent.getStringExtra("name");
? ? ? ? Toast.makeText(context,name,Toast.LENGTH_LONG).show();
? ? }
}

MySendBrocastReceiver.java

package com.example.intendbrocastreceiver;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
?
import org.w3c.dom.Text;
?
public class MySendBrocastReceiver extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_my_send_brocast_receiver);
?
? ? ? ? TextView text=(TextView)findViewById(R.id.name);//文本框?qū)ο?
? ? ? ? //獲取用戶名
? ? ? ? Intent getuser=getIntent();
? ? ? ? String s=getuser.getStringExtra("user");
? ? ? ? text.setText(s);
? ? ? ? //動態(tài)注冊廣播
? ? ? ? MyReceiver myreceicer=new MyReceiver();
? ? ? ? IntentFilter intentfilter=new IntentFilter();
? ? ? ? intentfilter.addAction("com.example.intentdbrocastreceiver.send");
? ? ? ? registerReceiver(myreceicer,intentfilter);
?
? ? ? ? Button but_send=(Button)findViewById(R.id.send);
? ? ? ? but_send.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? EditText text=(EditText)findViewById(R.id.text);
? ? ? ? ? ? ? ? String te=text.getText().toString();
?
? ? ? ? ? ? ? ? Intent intent=new Intent();
? ? ? ? ? ? ? ? intent.setAction("com.example.intentdbrocastreceiver.send");
? ? ? ? ? ? ? ? intent.putExtra("name",te);//傳遞
? ? ? ? ? ? ? ? sendBroadcast(intent);//發(fā)送廣播
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

MainActivity.java

package com.example.intendbrocastreceiver;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button signin=(Button)findViewById(R.id.signin);
? ? ? ? signin.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? EditText users=(EditText)findViewById(R.id.user);
? ? ? ? ? ? ? ? EditText passwords=(EditText)findViewById(R.id.password);
? ? ? ? ? ? ? ? //用戶輸入的用戶名密碼
? ? ? ? ? ? ? ? String user=users.getText().toString();
? ? ? ? ? ? ? ? String password=passwords.getText().toString();
? ? ? ? ? ? ? ? //系統(tǒng)內(nèi)包含的用戶名密碼
? ? ? ? ? ? ? ? String myuser="123";
? ? ? ? ? ? ? ? String mypassword="666";
? ? ? ? ? ? ? ? if(user.equals(myuser)&&password.equals(mypassword)){
? ? ? ? ? ? ? ? ? ? Intent login=new Intent();
? ? ? ? ? ? ? ? ? ? login.setAction("android.intent.action.sendbrocast");
? ? ? ? ? ? ? ? ? ? login.putExtra("user",user);//傳遞用戶名
? ? ? ? ? ? ? ? ? ? startActivity(login);
?
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"用戶名不存在或密碼錯誤",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? Button out=(Button)findViewById(R.id.signout);
? ? ? ? out.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之獲取SD卡及手機ROM容量的方法

    Android開發(fā)之獲取SD卡及手機ROM容量的方法

    這篇文章主要介紹了Android開發(fā)之獲取SD卡及手機ROM容量的方法,結(jié)合實例形式分析了Android針對SD卡的讀取及屬性操作相關(guān)技巧,需要的朋友可以參考下
    2016-04-04
  • Android中的OpenGL使用配置詳解

    Android中的OpenGL使用配置詳解

    這篇文章主要為大家介紹了Android中的OpenGL使用配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android ViewPager無限循環(huán)實現(xiàn)底部小圓點動態(tài)滑動

    Android ViewPager無限循環(huán)實現(xiàn)底部小圓點動態(tài)滑動

    這篇文章主要為大家詳細介紹了Android ViewPager無限循環(huán)實現(xiàn)底部小圓點動態(tài)滑動的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Andriod Service與Thread的區(qū)別介紹

    Andriod Service與Thread的區(qū)別介紹

    我們要明確Service是運行在主線程的,不能有耗時操作,這樣,在Service中處理耗時操作的時候,我們依然需要使用線程來處理,既然在Service里也要創(chuàng)建一個子線程,那為什么不直接在Activity里創(chuàng)建呢,下面通過本文給大家介紹Andriod Service與Thread的區(qū)別,一起看看吧
    2017-04-04
  • Android自定義扇形倒計時實例代碼

    Android自定義扇形倒計時實例代碼

    最近工作中需要做一個倒計時,是那種一個圓,慢慢的被吃掉的動畫倒計時,由于自己是android小白,效果還不是多滿意,先給大家分享實例代碼,僅供大家參考
    2017-03-03
  • Android編程實現(xiàn)簡單流量管理功能實例

    Android編程實現(xiàn)簡單流量管理功能實例

    這篇文章主要介紹了Android編程實現(xiàn)簡單流量管理功能的方法,結(jié)合實例形式分析了Android實現(xiàn)流量監(jiān)控所涉及的功能模塊與布局技巧,需要的朋友可以參考下
    2016-02-02
  • Android搜索框通用版

    Android搜索框通用版

    這篇文章主要為大家詳細介紹了Android搜索框通用版的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點總結(jié)

    Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點總結(jié)

    這篇文章主要介紹了Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點總結(jié),文中還給了一個RecyclerView布局管理的例子,需要的朋友可以參考下
    2016-04-04
  • Android Activity 橫豎屏切換的生命周期

    Android Activity 橫豎屏切換的生命周期

    這篇文章主要介紹了Android Activity 橫豎屏切換的生命周期的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • 詳解Android StrictMode嚴(yán)格模式的使用方法

    詳解Android StrictMode嚴(yán)格模式的使用方法

    這篇文章主要介紹了Android StrictMode嚴(yán)格模式的使用方法,需要的朋友可以參考下
    2018-01-01

最新評論