onReceive永远不会调用

问题描述:

下面是我的Java代码和我的XML代码。有人可以告诉我为什么我的onReceieve方法永远不会被调用。onReceive永远不会调用

的Java:

public class PopUPSMS extends Activity { 

    String RECEIVE_SMS = "RECEIVE_SMS"; 

    private static final String LOG_TAG = "PopUPSMS"; 

    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Log.i(LOG_TAG, "onCreate"); 

     registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.i(LOG_TAG, "onReceive"); 
      } 
     }, new IntentFilter(RECEIVE_SMS)); 
    } 
} 

XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.smith.johnathan.phone" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".PopUPSMS" 
      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> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest> 
+0

您需要为“android.provider.Telephony注册接收器。 SMS_RECEIVED”。我已经更新了你昨天提出的答案,并且描述了正确的方法。 – Zelimir 2011-01-31 20:39:44

registerReceiver(new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.i(LOG_TAG, "onReceive"); 
     } 
    }, new IntentFilter(RECEIVE_SMS)); 

,您已经注册以字符串 “许可权” 的广播接收器。 IntentFilter应该是一种行为形式。随着你的宣言:

static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 

,你将有:

registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.i(LOG_TAG, "onReceive"); 
      } 
     }, new IntentFilter(ACTION)); 

你可以看看Api demos Manifest的XML声明

+0

thieks cchenson它现在的作品!但我仍然无法获得警戒框 – 2011-01-31 22:25:31

不能使用接收器作为一个内部类。创建一个单独的。

+2

对不起弗拉基米尔我不认为你是对的。我发现了很多能够完成我的工作的例子。它必须是一些东西! – 2011-01-31 18:55:49

您是否需要指定要在清单中接收SMS?

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

    <receiver android:name=".MyApp"> 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

</application>