21

我正在尝试使用 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());
    }
}
4

6 回答 6

4

确保您实际上是在 Activity 或 Service 中创建和注册接收器,否则它不会被调用(我相信)。

一个非常简单的例子可能是:

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);
   }
}

我不能保证这会奏效,但我相信它会解决一些问题。如果您对事物有任何疑问,请在评论中提问。我相信你说它甚至没有被调用是正确的,因为你的接收器没有注册任何东西。如果您希望它在后台运行,请考虑使用服务。我真的希望这对您的努力有所帮助并祝您好运!

于 2011-12-07T19:33:40.383 回答
1

您只需要为接收者导出值 true。

<receiver android:name=".MyFirstApp" exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
</receiver>
于 2013-10-28T04:09:09.340 回答
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());
    }
}
于 2011-12-04T15:52:52.747 回答
0

如果你在背景中尝试“catch”,你可以看到这篇文章

"android.provider.Telephony.SMS_RECEIVED"服务很好。否则只有在活动生命周期中你才能“抓住”它

于 2016-01-19T15:48:00.403 回答
-1

这可能对你有帮助..试试这个..在广播接收器类

 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();
    }
于 2016-11-25T16:59:17.000 回答
-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();
}
于 2011-12-05T09:02:15.387 回答