我正在编写一个应用程序来收听传入的短信。我在网上检查了很多问题和答案,但它仍然无效。我的代码:
我的AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="superapp.networkapp">
<!--send and read SMS-->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait">
</activity>
<receiver android:name=".utility.tool.SmsReceiver" >
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".activity.StartTestActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
tools:replace="screenOrientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的工具/工具/ SmsReceiver.java
package superapp.networkapp.utility.tool;
import ...
public class SmsReceiver extends BroadcastReceiver {
//referenced from https://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("SMS", "onReceive: ");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] messages = null;
String sender;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
messages = new SmsMessage[pdus.length];
for(int i=0; i<messages.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]); //TODO
sender = messages[i].getOriginatingAddress();
String msgBody = messages[i].getMessageBody();
Toast.makeText(context,"Message: "+ msgBody + " from " + sender,Toast.LENGTH_LONG).show();
}
}
} catch(Exception e){
e.printStackTrace();
}
}
}
}
}
我在运行时运行askPermissions()
函数
private static final List<String> permissions = Arrays.asList(
Manifest.permission.SEND_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS);
private void askPermissions() {
if (!hasAllPermissions())
ActivityCompat.requestPermissions(this, permissions.toArray(new String[0]), 69);
}
private boolean hasPermission(String permission) {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
}
private boolean hasAllPermissions() {
for (String permission : permissions) {
if (!hasPermission(permission)) return false;
}
return true;
}
我有3个设备:使用Android 7.0的三星s7和索尼Xperia Z5 Compact,以及使用Android 6.0.1的三星s6。当我向这些设备发送消息时,只有默认的Messenger应用程序会显示通知。
我检查了其他人的答案和问题。似乎默认的Messenger应用程序具有更高的intentFilter
优先级,或者它们调用abortBroadcast()
。我还将我的意图的优先级设置为Android文档中定义的最大整数或999,但没有运气。
答案 0 :(得分:1)
你可以尝试这个,我也有同样的问题,通过添加权限修复。并且不需要以编程方式注册您的接收器,但您需要询问权限Manifest.permission.READ_SMS
<receiver
android:name=".name"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>