您好 stackoverflow 社区,
我想问你一个问题,因为我有一段时间被困住了,没有解决方案。
我几乎完成了我的 Android Studio 项目,我显示一个 Toast.makeText 并记录以检查短信的接收情况。它可以在带有短信发送模拟器的模拟器上运行。
但是当我将应用程序放在手机(小米或三星)上并且收到真正的短信时,makeText和日志都不会显示(我没有输入onReceive) 您有什么想法或者您遇到过同样的问题吗?
这是我的 AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MsgPreparer"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="SMSService"
android:label="Service de SMS MsgPreparer" />
<receiver android:name="SMSReceiver" android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
这是我的短信服务
public class SMSService extends Service {
private static final String TAG = "SMSService";
private boolean active;
private int countSMSReceived;
private SMSReceiver smsReceiver;
@Override
public void onCreate() {
super.onCreate();
active = true;
smsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, filter);
Log.d(TAG, "SMSService created and SMSReceiver registered");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "SMSService started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
Log.d(TAG, "SMSReceiver unregistered");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的短信接收器:
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "something is happening");
}
}
如果有人有任何想法/提示可以帮助我,我很高兴知道!
我试图阻止我在互联网上看到的其他问题,但一切似乎都很好(权限等......)
谢谢
在收到另一个人发来的短信后,它起作用了,我没有考虑它,也没有在其他线程上看到它,所以如果你有同样的问题,你应该尝试接收来自另一个联系人的短信!