我正在尝试在我的应用上接收短信,而我正在使用Android Studio的模拟器进行测试。在我看来,我已经写了我必须写的东西才能收到它们:
SMS_DELIVER_ACTION
设置了一个意图过滤器,其权限为BROADCAST_SMS
,优先级设置为999,SmsReceiver
的继承BroadcastReceiver
的类,并在其中添加了onCreate(Context, Intent)
函数。然而,当我通过模拟器的扩展控件发送短信时,名为SMSReceiver
的接收器似乎没有被调用...我知道它们有效,因为,当应用程序消息是默认的SMS应用程序,SMS显示在通知中。)
我看到的方式是我放在Toast
功能中的onReceive
没有显示出来。我还在Android Studio的onReceive
函数中添加了两个断点,但它们似乎无法到达。
(仿真设备是带有API 26 / Android O的Pixel XL。我在macOS 10.13 High Sierra上。)
我做错了什么?
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="st.thetechnologi.smscopy">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:minSdkVersion="19">
<receiver android:name=".SmsReceiver"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" >
</activity>
</application>
</manifest>
SmsReceiver.java
package st.thetechnologi.smscopy;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by l on 10/22/17.
*/
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "SmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, TAG, Toast.LENGTH_LONG).show();
}
}
谢谢!
编辑:好的,我需要手动从设置到我的应用程序授予短信权限。感谢Mike M.让我走上了正确的道路!