我正在做一个会说出来电者姓名的应用程序。它有一个主活动和两个广播接收器,用于监控来电和短信。从该接收器开始,它会启动一项服务,以说出呼叫者的姓名。
我面临的问题是
I want to start ans stop this broadcast receivers from the UI.i mean with the help of two buttons.
有可能吗,如果可以,我该怎么做?
我正在做一个会说出来电者姓名的应用程序。它有一个主活动和两个广播接收器,用于监控来电和短信。从该接收器开始,它会启动一项服务,以说出呼叫者的姓名。
我面临的问题是
I want to start ans stop this broadcast receivers from the UI.i mean with the help of two buttons.
有可能吗,如果可以,我该怎么做?
Sure. When you want to start it, call registerReceiver. When you want it off, call unregisterReceiver.
在按钮单击注册接收器:
YourReceiver mReceiver ;
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter filter = new IntentFilter(your action filter);
mReceiver = new YourReceiver();
registerReceiver(mReceiver, filter);
}
});
未注册的接收者:
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
unregisterReceiver(mReceiver);
}catch(IllegalArgumentException ex)
{
//your message.
}
}
});
希望这可以帮助...