我正在尝试使用android.provider.Telephony.SMS_RECEIVED来捕获传入的短信。
我构建了一个简单的应用程序,它适用于2.x,但是当我在我的4.0模拟器或设备上试用它时,它不起作用。
有什么想法吗?
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.giggsey.MyFirstApp" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<receiver android:name=".MyFirstApp">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
MyFirstApp.java
public class MyFirstApp extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "MyFirstApp";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
}
}
答案 0 :(得分:4)
确保您实际上是在活动或服务中创建并注册接收者,否则将无法调用(我相信)。
一个非常简单的例子可能是:
public class MyActivity extends Activity {
private BroadcastReceiver receiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
//Extends BroadcastReceiver
receiver = new MyFirstApp();
registerReceiver(receiver,filter);
}
//Also, to save headaches later
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
}
}
我不能保证这会起作用,但我相信它会解决一些问题。如果您对某些内容有任何疑问,请在评论中提问。我相信你是正确的说它甚至没有被调用,因为你的接收器没有注册任何东西。如果您希望它在后台运行,请考虑使用服务。我真的希望这对你的努力有所帮助,祝你好运!
答案 1 :(得分:1)
您只需要为接收者导出值true。
<receiver android:name=".MyFirstApp" exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
答案 2 :(得分:0)
我认为您的错误是您在包名中使用了类名。
在你的清单中,你在接收者中写了package="com.giggsey.MyFirstApp"
和<receiver android:name=".MyFirstApp">
。这意味着您的收件人的全名是com.giggsey.MyFirstApp.MyFirstApp
,但我相信它只是com.giggsey.MyFirstApp
。
如果我猜对了,您的清单中的com.giggsey.MyFirstApp
与com.giggsey
交换<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.giggsey" android:versionCode="1" android:versionName="1.0">
[...]
。
package com.giggsey;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyFirstApp extends BroadcastReceiver {
private static final String TAG = "MyFirstApp";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
}
}
还有这个:
{{1}}
答案 3 :(得分:0)
如果您在背景信息中尝试“捕获”,则可以看到this post。
"android.provider.Telephony.SMS_RECEIVED"
从服务中工作得很好。否则只有在活动生命周期中你才能“抓住”它
答案 4 :(得分:-1)
这可能会帮助你......在brodcast接收器类中
public static final String SMS_BUNDLE = "pdus";
public void onReceive(Context context, Intent intent)
{
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
smsBody = smsMessage.getMessageBody().toString();
address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
}
答案 5 :(得分:-2)
在On receive中请尝试添加以下行
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
if (intent.getAction() == SMS_RECEIVED) {
//any action you want here..
Toast.makeText(MyClass.this, "SMS RECEIVED",Toast.LENGTH_LONG).show();
}