无法从活动发送广播给其他:机器人 [英] unable to send Broadcast from activity to other: android
本文介绍了无法从活动发送广播给其他:机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有发送广播从一个活动到其他..its收到不工作我的code是below..pls是指它的问题..
发类是:
公共类SendBroadcast延伸活动{
公共静态字符串BROADCAST_ACTION =com.united coders.android.broadcasttest.SHOWTOAST/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮按钮1 =(按钮)findViewById(R.id.button1);
button1.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
sendBroadcast();
} });
}
公共无效sendBroadcast(){ 意向广播=新的Intent();
broadcast.setAction(com.united coders.android.broadcasttest.SHOWTOAST);
sendBroadcast(广播);
}
}
和reciving类是:
公共类ToastDisplay延伸活动{私人广播接收器接收器=新的广播接收器(){ @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.i(asdasd,sdasdasd);
// TODO自动生成方法存根
Toast.makeText(getApplicationContext(),%%%%%%%%%%%%接收,Toast.LENGTH_SHORT).show(); }
};@覆盖
保护无效onResume(){
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(接收器,过滤器); super.onResume();
}@覆盖
保护无效的onPause(){
unregisterReceiver(接收机);
super.onPause();
}
}
清单文件::::
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.united coders.android.broadcasttest
安卓版code =1
机器人:=的versionName1.0>
<采用-SDK安卓的minSdkVersion =4/> <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名字=。SendBroadcast
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=。ToastDisplay> &所述;意图滤光器>
<作用机器人:名字=com.united coders.android.broadcasttest.SHOWTOAST> < /作用>
&所述; /意图滤光器>
< /活性GT; < /用途>
< /清单>
解决方案
您尚未注册您的接收器在清单:注册为
<接收机器人:名字=接收器>
&所述;意图滤光器>
<作用
机器人:名字=com.united coders.android.broadcasttest.SHOWTOAST/>
&所述; /意图滤光器>
< /接收器>
i have problem with sending Broadcast receive from one activity to other ..its not working my code is below..pls refer to it ..
sending class is:
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);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendBroadcast();
}
});
}
public void sendBroadcast(){
Intent broadcast = new Intent();
broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST");
sendBroadcast(broadcast);
}
}
and reciving class is :
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("asdasd","sdasdasd");
// TODO Auto-generated method stub
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 file is ::::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<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>
</manifest>
解决方案
you have not registered your Receiver in Manifest :registered as
<receiver android:name="receiver">
<intent-filter>
<action
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>
这篇关于无法从活动发送广播给其他:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文