广播接收器未接收到广播

8

我正在尝试使用以下代码扩展Activity来广播一个toast消息。但是另一个Activity没有接收到广播,toast也没有显示出来。有人能解决我的错误吗?主要的活动是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 是指 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();
    }
}

manifest.xml如下所示:

<?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>
4个回答

14

广播可以分为两种类型:静态和动态。静态广播是在清单文件中声明的。动态广播可以在运行时声明。您将这两种广播类型合并为一个广播。

要声明一个简单的动态广播,您可以使用以下代码(基于您的代码)。当活动显示时,它将显示 Toast 消息。

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

现在你可以调用你的新活动而不是显示 toast。以下操作取决于您的需求(您想要做什么)。


好兄弟,你能告诉我该如何从清单中移除静态广播吗?还能给我一点提示,剩下的.java文件会是什么样的吗? - Ronak Mehta
这取决于你想要做什么。我已经向您展示了一个非常简单的示例,如何在接收广播后显示 Toast 消息。您可以简单地创建一个新项目并测试此类。 - Yury
首先,我想动态地显示Toast。 - Ronak Mehta
我提出的代码会在应用程序启动后显示toast。只需将此代码复制到一个新项目中,它就会向您显示一个toast。 - Yury
我已经解决了这个问题:@Override protected void onPause() { super.onPause(); //unregisterReceiver(br); sendBroadcast(); } - Ronak Mehta

3

ToastReceiver类在哪里?

<receiver android:name=".ToastReceiver">
    <intent-filter>
        <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>`

变更

public class ToastDisplay extends Activity {
    private BroadcastReceiver receiver = new BroadcastReceiver() {

}

to

public class ToastReceiver extends BroadcastReceiver {

}

0

尝试

<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>
    <intent-filter>
        <action android:name="android.intent.action.BROADCAST" />
    </intent-filter>
</activity>    

onCreate() 中调用。
sendBroadcast(v);

<action android:name="android.intent.action.BROADCAST" 是用来做什么的?看起来好像并不是必要的... - IgorGanapolsky
我认为这是用于测试-sendBroadcast会(希望)触发broadcastReciever。 - Hack5

-2
Button b1 = (Button)findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        try {
            String RESULT = new TestAsyncTask().execute(" ").get();
            System.out.println("RESULT "+RESULT);
        } 
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接