java - 广播接收器,¿我做对了吗?

标签 java android android-studio broadcastreceiver android-toast

第一个问题在这里... 我在大学里就有过这样的实践,但类(class)有时并不能解释所有内容,它是关于广播和接收 Toast 消息的。

(有些内容将以西类牙语显示)

你看,第一个应用程序是关于带有按钮的 View : Activity View

它所做的唯一事情是通过按钮发送消息,并且 OnClick 链接了 Activity 上的此方法,其名称为 Emisora.java (没有主 Activity,但它被配置为启动 Activity ):

public void Emision(View v){
        Intent intent = new Intent();
        intent.setAction("com.tecmilenio.practica91");
        intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
    }

list 是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tecmilenio.practica91">

    <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=".Emisora">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

然后我有另一个应用程序,它没有 Activity ,但它有一个广播接收器:

package com.tecmilenio.receptor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BroadcastReceptor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Transmission Recieved", Toast.LENGTH_SHORT).show();
    }
}

(我知道为此举杯并不是最佳实践,而只是为了类(class)的练习)

其 list 是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tecmilenio.receptor">

    <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">
        <receiver
            android:name=".BroadcastReceptor"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.tecmilenio.practica91"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

当我安装并执行第一个时,它什么也没发生,我点击按钮但什么也没有发生。

然后我后来看到,在第二个项目(接收器)中,当我从 Android Studio 执行它时,该应用程序没有出现在手机中,也没有执行任何操作,但我认为这是正常的,因为缺少其中的一项 Activity 。然后我还看到,通过工作室执行时,运行控制台也会向我发送消息“等待进程 (com.tecmilenio.receptor) 出现在 xiaomi-mi_8-2ef63c6e 上时超时。”,也许这有一些东西是(?)

如果有人向我解释这一点,我将不胜感激...谢谢!

更新

我发现问题出在启动时超时,添加一个没有任何内容的 Activity 并启动该 Activity 使其工作,但这不是它应该做的,它应该是应该的'没有任何 Activity ,并且应该在“practica91”应用程序发送消息时执行 OnRecieve... 有什么办法可以做到这一点吗?

最佳答案

在 list 中声明的​​设置操作和从 Activity 发送广播是不同的。

尝试在两个地方使用相同的操作 -

 Intent intent = new Intent();
 intent.setAction("com.tecmilenio.practica91");
 intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
 sendBroadcast(intent);

关于java - 广播接收器,¿我做对了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60733296/

相关文章:

android - 在我的项目中添加阴影生成的 jar 时出现重复条目​​异常

android - ndk-build 失败,process_begin : CreateProcess(NULL, uname -a, ...) 失败

java - 用 Glide 加载图像很慢

android-studio - 无法在 Android Studio 中创建 java 类文件

java - 包含扩展类的抽象类列表

android - 如何从可以输入 Spring Security 安全 Web 服务方法的 Android 应用发出请求?

android - Android 和 iOS 之间关于数据消息的 FCM 差异

java - 条目 fakeoutputdirectory=null 中出现错误 null 值?

javax.net.ssl.SSLHandshakeException : Remote host closed connection during handshake during web service communicaiton

java - 创建连接到 HBaseTestingUtility 的 HBaseAdmin