实现步骤:

  1. 在Activity布局文件中添加一个呼救按钮。

  2. 在Activity中编写相应的代码,当呼救按钮被点击时,发送求救广播消息。

    // 发送求救广播消息
    Intent intent = new Intent();
    intent.setAction("com.example.SOS");
    intent.putExtra("name", "小明");
    intent.putExtra("message", "我陷入危险,请求帮助!");
    sendBroadcast(intent);
    
  3. 创建一个广播接收者,在其中编写相应的代码,等待呼救的广播信号。

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.example.SOS")) {
                String name = intent.getStringExtra("name");
                Toast.makeText(context, name + "请放心,我这就去救援!", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
  4. 在AndroidManifest.xml文件中注册广播接收者。

    <receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="com.example.SOS"/>
        </intent-filter>
    </receiver>
    
  5. 在Activity的onCreate()方法中,创建广播接收者的实例,并注册广播接收者。

    // 创建广播接收者的实例
    MyReceiver receiver = new MyReceiver();
    // 注册广播接收者
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.example.SOS");
    registerReceiver(receiver, intentFilter);
    
  6. 在Activity的onDestroy()方法中,注销广播接收者。

    unregisterReceiver(receiver);
    

完整代码如下:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button sosBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sosBtn = findViewById(R.id.sos_btn);
        sosBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 发送求救广播消息
                Intent intent = new Intent();
                intent.setAction("com.example.SOS");
                intent.putExtra("name", "小明");
                intent.putExtra("message", "我陷入危险,请求帮助!");
                sendBroadcast(intent);
            }
        });

        // 创建广播接收者的实例
        MyReceiver receiver = new MyReceiver();
        // 注册广播接收者
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.SOS");
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播接收者
        unregisterReceiver(receiver);
    }
}

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.example.SOS")) {
            String name = intent.getStringExtra("name");
            Toast.makeText(context, name + "请放心,我这就去救援!", Toast.LENGTH_SHORT).show();
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sosdemo">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <!-- 注册广播接收者 -->
        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="com.example.SOS"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
实验要求:一个Activity上有一个呼救按钮点击此按钮会发出一个求救广播消息消息的内容是:name小明;message我陷入危险请求帮助!提示:此消息可以通过Intent来传递与Activity传递数据类似。创建一个广播接收者用于等待呼救的广播信号。当它接收到求救的广播消息后取出消息中的name弹出Toast内容为:小明请放心我这就去救援!提示:name从onReceive的入参intent中获

原文地址: https://www.cveoy.top/t/topic/bTCP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录