广播接收者,¿我做对了吗?

问题描述 投票:0回答:2

这里的第一个问题...我上大学时有这种做法,但是该课程有时无法解释所有内容,而是广播和接收Toast消息。

(某些内容将使用西班牙语)

[您看到的,第一个应用程序是关于带有按钮的视图的:Activity View

它唯一要做的就是通过按钮发送消息,并且OnClick已链接了Activity上的此方法,其名称为Emisora.java(没有主要活动,但已对其进行了配置将其作为启动活动):

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

清单是:

<?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>

然后,我有另一个应用程序,它没有活动,但是确实有广播接收者:

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();
    }
}

(我知道为此做个吐司不是最佳实践,而只是本课程的实践)

其清单为:

<?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上。”,也许这有点成为(?)

[我会很高兴有人向我解释这个……谢谢!

java android android-studio broadcastreceiver android-toast
2个回答
0
投票

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

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

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

0
投票

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

<action android:name="com.tecmilenio.practica91"/>

第一个更改是将广播操作重命名为com.tecmilenio.emision

   <receiver
            android:name=".BroadcastReceptor"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.tecmilenio.emision"/>
            </intent-filter>
        </receiver>

[另一个重要点是,当向应用程序发送广播添加FLAG_INCLUDE_STOPPED_PACKAGES标志时,因为从应用A广播到应用B时,应用B可能未运行,所以此标志可确保即使应用未运行,广播也可以播放:

关于我也是墨西哥人!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.