广播接收器不接收广播 [英] BroadcastReceiver Not receiving Broadcast

查看:200
本文介绍了广播接收器不接收广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与播出下列code扩展活动敬酒消息。不过,广播没有被其他活动收到,不显示敬酒。谁能解决我的错误?主要活动是的 SendBroadcast.java

 进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;公共类SendBroadcast延伸活动{    公共静态字符串BROADCAST_ACTION =
                             com.united coders.android.broadcasttest.SHOWTOAST    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }    公共无效sendBroadcast(视图v){
        意向广播=新的Intent();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(广播);
    }
}

吐司显示活动的 ToastDisplay.java

 进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.widget.Toast;公共类ToastDisplay延伸活动{    私人广播接收器接收器=新的广播接收器(){        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            Toast.makeText(getApplicationContext()获得,
                    Toast.LENGTH_SHORT).show();
        }
    };    @覆盖
    保护无效onResume(){
        IntentFilter的过滤器=新的IntentFilter();
        filter.addAction(SendBroadcast.BROADCAST_ACTION);
        registerReceiver(接收器,过滤器);
        super.onResume();
    }    @覆盖
    保护无效的onPause(){
        unregisterReceiver(接收机);
        super.onPause();
    }
}

manifest.java 如下:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.broad
    安卓版code =1
    机器人:=的versionName1.0>
    <采用-SDK安卓的minSdkVersion =3/>
    <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:名字=。SendBroadcast
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <接收机器人:名字=。ToastReceiver>
            &所述;意图滤光器>
                <作用机器人:名字=com.united coders.android.broadcasttest.SHOWTOAST/>
            &所述; /意图滤光器>
        < /接收器>
    < /用途>
< /清单>


解决方案

有可能是两种类型的broacast:静态和动态。静态是那些在manifest文件中声明。动态可以在运行时声明。您可以在一个广播结合这两种类型的广播。

要声明,你可以使用下面的code(即根据您的code)一个简单的动态播出。它只会显示敬酒消息显示活动时。

 进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.Toast;公共类BroadcastTestActivity延伸活动{    公共静态字符串BROADCAST_ACTION =
                            com.united coders.android.broadcasttest.SHOWTOAST
    广播接收器BR =新的广播接收器(){        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            Log.w(检查,里面有关于接收器);
            Toast.makeText(getApplicationContext()获得,
                    Toast.LENGTH_SHORT).show();
        }
    };    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        IntentFilter的过滤器=新的IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(BR,过滤器);
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        sendBroadcast();
    }    @覆盖
    保护无效的onPause(){
        super.onPause();
        unregisterReceiver(BR);
    }    公共无效sendBroadcast(){
        意向广播=新的Intent();
        broadcast.setAction(BROADCAST_ACTION);
        broadcast.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(广播);
    }
}

因此​​,现在显示敬酒你可以打电话给你的新的活动。下面的操作取决于你的需求(你想做的事)。

I am trying to broadcast a toast message with the following code extending Activity. But the broadcast is not received by another Activity, the toast is not displayed. Can someone solve my error? The main activity is SendBroadcast.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SendBroadcast extends Activity {

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

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

Toast Display Activity is ToastDisplay.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

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

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

and manifest.java is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.broad"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:icon="@drawable/ic_launcher"
        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>
        <receiver android:name=".ToastReceiver" >
            <intent-filter>
                <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

解决方案

There can be two types of broacast: static and dynamic. Static are those that are declared in the manifest file. Dynamic can be declared during runtime. You combined these two types of broadcast in one broadcast.

To declare a simple dynamic broadcast you can use the following code (that is based on your code). It will simply display toast message when activity is shown.

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BroadcastTestActivity extends Activity {

    public static String BROADCAST_ACTION =     
                            "com.unitedcoders.android.broadcasttest.SHOWTOAST";
    BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.w("Check", "Inside On Receiver");
            Toast.makeText(getApplicationContext(), "received",
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(br, filter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sendBroadcast();
    }

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

    public void sendBroadcast() {
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        broadcast.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(broadcast);
    }
}

So now instead of showing toast you can call your new activity. The following actions depend on your needs (what you want to do).

这篇关于广播接收器不接收广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆