我想问一个问题
我已经制作了一个启动和停止后台服务的按钮
我创建了一个课程SmsReceiver
来读取传入的短信
所以我希望当服务开始调用Sms BroadcastReceiver
时
任何想法或任何其他想法如何阅读带有后台服务的短信?
答案 0 :(得分:0)
如果您已经SmsReceiver
正常工作,那么您需要做的就是从Service
注册。要做到这一点,您需要覆盖Service
的{{1} }}
您的服务应该如下所示:
onCreate()
在清单中,请确保包含必要的SMS权限,并记住在清单中声明public class SmsService extends Service
{
private SmsReceiver receiver; //global so we can unregister it when the time comes.
public void onCreate ()
{
super.onCreate ();//call to super
//make a new intent filter so that all incoming SMS get to you as well
IntentFilter smsFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new SmsReceiver(); //make a new SmsReceiver
registerReceiver(receiver, smsFilter); //register the Receiver
}
public void onDestroy ()
{
unregisterReceiver (receiver); //unregisters the Receiver if Service is killed
super.onDestroy();
}
}
。
Service