收到短信时,我无法捕捉到意图。以下是我的服务代码。我能够抓住“Intent.ACTION_SCREEN_ON
”和“Intent.ACTION_SCREEN_OFF
”,但我无法抓住SMS_RECEIVED
意图“android.provider.Telephony.SMS_RECEIVED
”。
真诚地感谢我在这里做错了什么提示吗?
public class SmsCatcher extends Service{
BroadcastReceiver myBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("myBroadcast SmsCatcher", "Entered onReceive method");
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("myBroadcast SmsCatcher", "Caught SCREEN_OFF");
}
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("myBroadcast SmsCatcher", "Caught SCREEN_ON");
}
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Log.i("myBroadcast SmsCatcher", "SMS_RECEIVED");
}
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("neil SmsCatcher", "Entered onCreate() in 'SmsCatcher extends Service'");
Toast.makeText(this, "Entered onCreate() in 'SmsCatcher extends Service'", Toast.LENGTH_LONG).show();
registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("neil SmsCatcher", "Entered onDestroy() in 'SmsCatcher extends Service'");
Toast.makeText(this, "Entered onDestroy() in 'SmsCatcher extends Service'", Toast.LENGTH_LONG).show();
}
}
更新1 :从我正在进行的调试中添加更多信息: 我使用App Internal Broadcasts Monitor看到收到短信时有广播,我什么都看不到,非常奇怪。可能是什么原因? 我安装了其他短信应用(环聊,AT& T信息) - 这些无法抑制广播吗?
更新2:发现问题,但不知道如何解决 我卸载了Google Hangouts (replaces Talk)应用程序并且工作正常! 围绕这个解决方案吗? (我应该根据stackoverflow规则打开一个单独的线程吗?)
更新3:找到ROOTCAUSE (当然有下面的帮助,谢谢) 事实证明,由于新的Google Hangouts App,我需要setPriority(int)。我在Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app
找到了解决方案答案 0 :(得分:3)
您需要注册android.provider.Telephony.SMS_RECEIVED
意图过滤器,只有在您注册后才能检查onRecive
中的意图
IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(myBroadcastReceiver, filter);
更新问题: 事实证明,Google Hangouts App会在收到短信后立即中止短信广播,因此禁用环聊2.0中的短信支持可能会解决问题。
答案 1 :(得分:2)
您没有注册intent-filter:
@Override
public void onCreate() {
// ..
registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(myBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(myBroadcast, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); // missing in your code
}
请在android.permission.RECEIVE_SMS
中查看AndroidManifest.xml
的权限。