通过活动呼叫广播接收者

发布于 2024-11-28 02:38:39 字数 2057 浏览 0 评论 0原文

我是 Android 开发的新手。我正在尝试从我的活动中发明一个广播接收器。

public class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"ABC", Toast.LENGTH_LONG).show();
        Bundle extras = intent.getExtras();
        String[] parameters= (String[])intent.getSerializableExtra("parameters");
    }
}

我的活动是

public class MyActivity extends Activity {
    public static String BROADCAST_ACTION="com.kiosk.cbal.CALL_RECEIVER";

    /**
     * @see android.app.Activity#onCreate(Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String parameters = "safdsam,fdsa,fdsa,fdsa,fdsa";
        String[] parameters =abc.split(",");
        Intent i = new Intent("com.package.MyReceiver");
        i.putExtra("parameters", parameters);
        sendBroadcast(i);
    }
}

我的清单文件是

<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
    package="com.package" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="7"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:label="@string/app_name" android:name="MyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver android:name="MyReceiver">
            <intent-filter>
                <action android:name="com.kiosk.cbal.CALL_RECEIVER"/>
            </intent-filter>
        </receiver> 

    </application>
    <uses-permission android:name="android.permission.BATTERY_STATS"/>
</manifest>

现在我如何能够通过活动调用广播接收器?

I am a newbie to Android development. I am trying to invent a broadcast receiver from my activity.

public class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"ABC", Toast.LENGTH_LONG).show();
        Bundle extras = intent.getExtras();
        String[] parameters= (String[])intent.getSerializableExtra("parameters");
    }
}

my activity is

public class MyActivity extends Activity {
    public static String BROADCAST_ACTION="com.kiosk.cbal.CALL_RECEIVER";

    /**
     * @see android.app.Activity#onCreate(Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String parameters = "safdsam,fdsa,fdsa,fdsa,fdsa";
        String[] parameters =abc.split(",");
        Intent i = new Intent("com.package.MyReceiver");
        i.putExtra("parameters", parameters);
        sendBroadcast(i);
    }
}

My Manifest file is

<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
    package="com.package" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="7"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:label="@string/app_name" android:name="MyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver android:name="MyReceiver">
            <intent-filter>
                <action android:name="com.kiosk.cbal.CALL_RECEIVER"/>
            </intent-filter>
        </receiver> 

    </application>
    <uses-permission android:name="android.permission.BATTERY_STATS"/>
</manifest>

Now how I'll Be able to give a call to broadcast receiver through an activity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-12-05 02:38:39

删除操作名称中的空格。创建 Intent 时使用该名称。

Remove the space from the action name. Use the name when you create the Intent.

风蛊 2024-12-05 02:38:39

更新:上面的代码仍然有Intent i = new Intent("com.package.MyReceiver");。它应该是 Intent i = new Intent("com.kiosk.cbal.CALL_RECEIVER");

同时,在您的清单中,接收者名称被指定为

无论如何,您应该检查 Android 日志文件,以了解有关您正在广播的意图出现问题的详细信息。


您传递给 Intent 构造函数的字符串必须与清单中接收者的操作元素指定的字符串相同。您正在使用接收器类名构造 Intent,但您的接收器被声明为侦听 VIEW 意图。

Update: The code above still has Intent i = new Intent("com.package.MyReceiver");. It should be Intent i = new Intent("com.kiosk.cbal.CALL_RECEIVER");

Meanwhile, in your manifest the receiver name is specified as <receiver android:name="MyReceiver">. note that android:name must be either a fully qualified class name, or a name relative to the package name. The way PackageManager distinguishes the two is by the presence of a . at the beginning of the name. Thus, with your declaration, PackageManager is most likely attempting to instantiate MyReceiver' instead ofcom.package.MyReceiver`.

In any case, you should check the Android log file for details on what's going wrong with the intent you're broadcasting.


The string that you pass to the Intent constructor must be the same as the string specified for the action element for your receiver in the manifest. You are constructing the Intent with your receiver class name, but your receiver is declared to listen to the VIEW intent instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文