在活动的Andr​​oid的BroadcastReceiverAndr、oid、BroadcastReceiver

2023-09-12 04:07:32 作者:陪你到世界尽头

我只是想这个小样本项目,它的作用: 活动一具有发送广播的按钮。活动二显示接收时举杯。 下面是code,广播永远不会收到。我该怎么办错了?

发送广播的

 公共类SendBroadcast延伸活动{

    公共静态字符串BROADCAST_ACTION =com.united coders.android.broadcasttest.SHOWTOAST;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }

    公共无效sendBroadcast(视图v){
        意图广播=新意图();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(广播);
    }
}
 

接收它的

 公共类ToastDisplay延伸活动{

    私人BroadcastReceiver的接收器=新的BroadcastReceiver(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            Toast.makeText(getApplicationContext()获得,Toast.LENGTH_SHORT);
        }
    };

    @覆盖
    保护无效onResume(){
        IntentFilter的过滤器=新的IntentFilter();
        filter.addAction(SendBroadcast.BROADCAST_ACTION);
        registerReceiver(接收机,过滤器);
        super.onResume();
    }

    @覆盖
    保护无效的onPause(){
        unregisterReceiver(接收机);
        super.onPause();
    }
}
 

清单

 <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
    <活动机器人:SendBroadcast名称=机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名称=。ToastDisplay>
        <意向滤光器>
            <作用机器人:名称=com.united coders.android.broadcasttest.SHOWTOAST>< /作用>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>
 

解决方案   

我该怎么办错了?

ToastDisplay的来源$ C ​​$ c是OK(我的是相似的作品),但它只会收到东西,如果它是目前在前台(你在onResume注册接收器)。但是,如果不同的活性(在本例SendBroadcast活性)示出它不能接收任何东西。

相反,你可能想startActivity从第一个活动ToastDisplay?

的BroadcastReceiver和活动意义在不同的使用情况。 在我的应用程序我需要从后台GPS跟踪服务,接收通知,并告诉他们在活动(如果活动是在< STRONG>前景)。

有没有必要为的注册清单中的接收器的。它甚至会的的危害的在我使用的情况下 - 我的接收器操纵活动的用户界面,如果该活动没有当前显示的用户界面不会的onReceive时可供使用。相反,我注册并在描述注销接收机在onResume和活动的onPause BroadcastReceiver文档:

  

您可以动态地Context.registerReceiver注册这个类的一个实例()或静态地通过你的Andr​​oidManifest.xml中标记发布实施。

I'm just trying this little sample project, all it does: Activity one has a Button that sends a Broadcast. Activity two displays a toast when received. Below is the code, the Broadcast is never received. What do I do wrong?

Sending the Broadcast

public class SendBroadcast extends Activity {

    public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void sendBroadcast(View v){
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(broadcast);
    }
}

Receiving it

public class ToastDisplay extends Activity {

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT);
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SendBroadcast.BROADCAST_ACTION);
        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }
}

Manifest

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SendBroadcast" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ToastDisplay">
        <intent-filter>
            <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"></action>
        </intent-filter>
    </activity>
</application>

解决方案

What do I do wrong?

The source code of ToastDisplay is OK (mine is similar and works), but it will only receive something, if it is currently in foreground (you register receiver in onResume). But it can not receive anything if a different activity (in this case SendBroadcast activity) is shown.

Instead you probably want to startActivity ToastDisplay from the first activity?

BroadcastReceiver and Activity make sense in a different use case. In my application I need to receive notifications from a background GPS tracking service and show them in the activity (if the activity is in the foreground).

There is no need to register the receiver in the manifest. It would be even harmful in my use case - my receiver manipulates the UI of the activity and the UI would not be available during onReceive if the activity is not currently shown. Instead I register and unregister the receiver for activity in onResume and onPause as described in BroadcastReceiver documentation:

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.