首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

自定义的静态广播没有作用怎么处理

2013-10-30 
自定义的静态广播没有作用怎么办?//在AndroidManifest.xml里注册静态广播receiver android:name.TestBr

自定义的静态广播没有作用怎么办?


//在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系统,真机和模拟器都测试过 广播 Broadcast android broadcastreceiver
[解决办法]
sendBroadcast(new Intent("com.lpj.test"));
写错了。

这样写
sendBroadcast(new Intent(this,TestBroadcast.class));
[解决办法]
引用:

//在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系统,真机和模拟器都测试过



    Intent intent = new Intent();    
    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, "com.lpj.test"));

或者
sendBroadcast(new Intent(replyer.this.getApplicationContext(), "com.lpj.test"));

楼上说的也都行!

热点排行