通过活动呼叫广播接收者
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除操作名称中的空格。创建 Intent 时使用该名称。
Remove the space from the action name. Use the name when you create the Intent.
更新:上面的代码仍然有
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 beIntent i = new Intent("com.kiosk.cbal.CALL_RECEIVER");
Meanwhile, in your manifest the receiver name is specified as
<receiver android:name="MyReceiver">
. note thatandroid:name
must be either a fully qualified class name, or a name relative to the package name. The wayPackageManager
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 instantiateMyReceiver' instead of
com.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 theIntent
with your receiver class name, but your receiver is declared to listen to theVIEW
intent instead.