我已经尝试了很多(如果不是全部...)接收短信示例,但在调用 onReceive() 之前它总是失败。
CatLog:
10-26 20:05:30.990: INFO/System.out(2714): INFO: Received message
10-26 20:05:30.998: INFO/System.out(2714): INFO: Message body: Lmjgk
10-26 20:05:31.021: WARN/ActivityManager(1317): Unable to launch app org.apache.sms/10166 for broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) }: process is bad
10-26 20:05:31.021: WARN/ActivityManager(1317): finishReceiver called but none active
当与另一个 BroadcastReceiver android.intent.action.PHONE_STATE 一起实现时,后者正在被调用,并且在 SMS 失败时工作得很好。
为了简化它,我刚刚创建了 hello 示例并更新了 SMS BroadcastReceiver 的 list 和一个文件。
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byp.sms" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.byp.sms.SMSReceiver">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
广播接收器
package com.byp.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class SMSReceiver extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SMSReceiver", "onReceive"); // <<=== It does not even get here....
if (intent != null && intent.getAction() != null
&& ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
Log.d("SMSReceiver", "SMSReceived " + pduArray.toString());
}
Log.d("SMSReceiver", "onReceive...Done");
}
}
Hello Activity:生成的代码没有更改
package com.byp.sms;
import android.app.Activity;
import android.os.Bundle;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我从设备中清理了所有相关应用程序、卸载了它、重新启动了设备等等,这并没有帮助。
任何建议或提示将不胜感激!
谢谢
最佳答案
如果您在 AndroidManifest 文件中注册接收器,则需要将其设置为静态。 如果接收者不是静态的,那么你需要通过代码注册。请执行以上操作,它将显示出一些积极的结果。
关于android - Android 上无法接收短信(进程错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911656/