我正在编写一个接收 SMS 数据消息、加密其内容并将其保存到数据库的应用程序。为了实现它,我创建了一个带有本地 BroadcastReceiver 的服务,如下所示:
public class SMMReceiverService extends Service {
private class SMMreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
//call a method from service
}
}
private SMMreceiver smmReceiver;
private IntentFilter intentFilter;
@Override
public void onCreate(){
super.onCreate();
android.os.Debug.waitForDebugger();
smmReceiver = new SMMreceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.DATA_SMS_RECEIVED");
intentFilter.addDataScheme("sms");
intentFilter.addDataAuthority("localhost","8901");
registerReceiver(smmReceiver, intentFilter);
}
}
服务正常启动,但从未调用 SMMreceiver 的 onReceive 方法。在清单中,我只声明了我的服务如下:
<service
android:name=".Services.SMMReceiverService"
android:enabled="true"
android:exported="true" >
</service>
还有所有必需的权限:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
如何以正确的方式解决我的问题?我将不胜感激您的帮助!