//在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

解决方案 »

  1.   

    sendBroadcast(new Intent("com.lpj.test"));
    写错了。这样写
    sendBroadcast(new Intent(this,TestBroadcast.class));
      

  2.   

        Intent intent = new Intent();    
        intent.setAction("com.lpj.test");
        sendBroadcast(intent);
      

  3.   


    Intent intent = new Intent();    
        intent.setAction("com.lpj.test");
        sendBroadcast(intent);像楼上说的这样就可以,如果你的receiver不是在activity里面你把context 改成getApplication().getContext();
      

  4.   

    sendBroadcast(new Intent(replyer.this, "com.lpj.test"));
    或者
    sendBroadcast(new Intent(replyer.this.getApplicationContext(), "com.lpj.test"));
    楼上说的也都行!
      

  5.   

    不好意思哈,这是我粗心造成的错误,我把<Register>放在<Actiity>...</Activity>里了,正确的应该是放在<Application>...</Application>里