//在AndroidManifest.xml里注册静态广播
<receiver android:name=".TestBroadcast">
<intent-filter>
<action android:name="com.lpj.test"/>
</intent-filter>
</receiver>//定义广播接收类
public class TestBroadcast extends BroadcastReceiver {
public static final String RECEIVER = "com.lpj.test";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVER)){
Log.i("BroadcastTest", "Broadcast Start!");
Toast.makeText(context, "Broadcast Start!",
Toast.LENGTH_LONG).show();
}
}
}//在主界面里发送广播
public class replyer extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replyer);
this.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadcast(new Intent("com.lpj.test"));
}
});
}
}
结果没有任何反应(既不弹出Toast,logcat里也没东西),4.2系统,真机和模拟器都测试过广播Broadcastandroidbroadcastreceiver
解决方案 »
- 关于onDoubleTap事件
- 点击添加按钮,添加应用程序,添加删除按钮,删除应用程序
- android系统手机不root能否授权??
- 计算器写到这里卡壳了,求解!!!
- 关于SearchView控件的一点问题
- 如何在google map中得到周边信息,比如餐厅,加油站等
- HTC Hero 使用的是什么版本的Android系统?
- 求助,伪装来电或来短信的功能是怎么实现的?
- 弱弱问句android手机,自己的本地资料存在CONTACTS2数据库哪里?
- android 使用CursorAdapter如何监听数据库变化
- android 4.0 USER_PRESENT监听不到??
- Cursor getString问题
写错了。这样写
sendBroadcast(new Intent(this,TestBroadcast.class));
intent.setAction("com.lpj.test");
sendBroadcast(intent);
Intent intent = new Intent();
intent.setAction("com.lpj.test");
sendBroadcast(intent);像楼上说的这样就可以,如果你的receiver不是在activity里面你把context 改成getApplication().getContext();
或者
sendBroadcast(new Intent(replyer.this.getApplicationContext(), "com.lpj.test"));
楼上说的也都行!