Android:BroadcastReceiver SMS_RECEIVED

时间:2012-02-11 10:37:06

标签: java android android-intent broadcastreceiver

我正在尝试使用BroadcastReceiver进行标题中的操作。 我创建了2个类: 1)注册我的BroadcastReceiver的活动。 2)扩展BroadcastReceiver并覆盖onReceive方法的类。

Manifest显然没有任何错误,所以我决定在这里发布它,也是两个类。

感谢您的关注。 P.S =我不是英国人,如果我的问题有任何语法错误,那就很抱歉;)

public class SMSBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "SMSBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {

if (intent.getAction().equalsIgnoreCase("SMS_RECEIVED")){
    Log.d(TAG, "Un messaggio ricevuto");
    Toast.makeText(context, "Un messaggio ricevuto", Toast.LENGTH_LONG).show();
    }
   }
 }



public class SMSyncHome extends Activity {

private SMSBroadcastReceiver SMSreceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SMSreceiver = new SMSBroadcastReceiver();
    registerReceiver(SMSreceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
   }

       }


    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.contervis.SMSync"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity android:name=".SMSyncHome"
                  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=".SMSBroadcastReceiver" android:enabled="true">
            <intent-filter >
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您在BroadcastReceiver中匹配的操作应为android.provider.Telephony.SMS_RECEIVED,而不仅仅是最后一部分。