Android 广播短信接收器无法启动
Android Broadcast SMS receiver won't launch
我正在尝试构建一个应用程序,该应用程序在将短信发送到 phone 后通过电子邮件发送图片。所以我一直在关注 this Tutorial 但没有成功。在调试模式下向真实 phone 发送短信时,没有任何反应。我一直在尝试调试应用程序,但广播接收器甚至没有启动。
public class SmsReciever extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "started…");
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReciever", "senderNum: " + senderNum + "; message" + message);
//action
}
}
} catch (Exception e){
Log.e("SmsReceiver", "Exception sms Reciever" + e);
}
Logcat 不打印该代码位的任何错误或日志。我的清单文件如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tapplic.adrianopaulus.smscamera" >
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".SmsReciever" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我也在Whosebug上看过这些问题:
Android - SMS Broadcast receiver
Android SMS reciever not working
权限字符串区分大小写。你有:
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS"></uses-permission>
你应该有:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
编辑:添加额外的问题描述:
此外,您 <intent-filter>
中的 ACTION 拼写错误。你有:
<action android:name="android.provider.Telephony.SMS_RECIEVED" />
然而,这应该是:
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
英文规则是"I before E, except after C" :-)
我正在尝试构建一个应用程序,该应用程序在将短信发送到 phone 后通过电子邮件发送图片。所以我一直在关注 this Tutorial 但没有成功。在调试模式下向真实 phone 发送短信时,没有任何反应。我一直在尝试调试应用程序,但广播接收器甚至没有启动。
public class SmsReciever extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "started…");
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReciever", "senderNum: " + senderNum + "; message" + message);
//action
}
}
} catch (Exception e){
Log.e("SmsReceiver", "Exception sms Reciever" + e);
}
Logcat 不打印该代码位的任何错误或日志。我的清单文件如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tapplic.adrianopaulus.smscamera" >
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".SmsReciever" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECIEVED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我也在Whosebug上看过这些问题:
Android - SMS Broadcast receiver
Android SMS reciever not working
权限字符串区分大小写。你有:
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS"></uses-permission>
你应该有:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
编辑:添加额外的问题描述:
此外,您 <intent-filter>
中的 ACTION 拼写错误。你有:
<action android:name="android.provider.Telephony.SMS_RECIEVED" />
然而,这应该是:
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
英文规则是"I before E, except after C" :-)