Java 广播接收机&xBF;我做得对吗?

Java 广播接收机&xBF;我做得对吗?,java,android,android-studio,broadcastreceiver,android-toast,Java,Android,Android Studio,Broadcastreceiver,Android Toast,这里的第一个问题。。。 我在我的大学里有过这样的练习,但这门课有时并不能解释一切,而是关于广播和接收祝酒词 (有些东西将用西班牙语) 你看,第一个应用程序是关于用按钮查看的: 它所做的唯一一件事是通过按钮发送消息,OnClick链接了活动上的此方法,该方法的名称为Emisora.java(没有主活动,但它被配置为启动活动): 舱单是: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://

这里的第一个问题。。。 我在我的大学里有过这样的练习,但这门课有时并不能解释一切,而是关于广播和接收祝酒词

(有些东西将用西班牙语)

你看,第一个应用程序是关于用按钮查看的:

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

舱单是:

<?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>
<?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>
(我知道为这项活动干杯不是最好的做法,只是为了这门课程的实践)

其清单是:

<?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>
<?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执行它时,该应用程序不会出现在手机上,也不会执行任何操作,但我认为这是正常的,因为其中没有活动。然后我还看到,当通过studio执行时,运行控制台也会向我发送消息“等待进程(com.tecmilenio.receptor)出现在xiaomi-mi_8-2ef63c6e上时超时”。也许这有点不对劲(?)

我希望有人能给我解释一下。。。谢谢

更新

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

设置清单中声明的操作和从活动发送广播是不同的

尝试在两个位置使用相同的操作-

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

我认为这里有一个小错误。您将在第一个应用程序中作为操作发送 以下字符串
com.tecmilenio.emision
。而在第二个应用程序中,您正在注册广播的操作,如下所示

<action android:name="com.tecmilenio.practica91"/>
另一个重要的一点是,当发送广播添加
标志\u INCLUDE\u STOPPED\u PACKAGES
标志到intent时,因为当您从应用程序A广播到应用程序B时,应用程序B可能没有运行,此标志确保即使应用程序没有运行,广播也会传播出去:


你好,我也是墨西哥人

好吧,有趣的是,发出辐射的应用程序A被称为Practica 9.1,所以包是com.tecmilenio.practica91,应用程序B被称为Receptor,包是com.tecmilenio.Receptor。。。即使我做了另一个名为Emissor的项目,它也不起作用,但是它确实执行了一个系统。如果我在方法emission中插入它,那么该应用程序确实起作用,而我认为,它不起作用的是受体应用程序。。。“再见,朋友,格雷西亚斯!我不太明白,我已经写了那个代码,或者,你有什么建议?
   <receiver
            android:name=".BroadcastReceptor"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.tecmilenio.emision"/>
            </intent-filter>
        </receiver>