2011-01-31 206 views
0

下面是我的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

回答

2
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

0

不能使用接收器作爲一個內部類。創建一個單獨的。

+2

對不起弗拉基米爾我不認爲你是對的。我發現了很多能夠完成我的工作的例子。它必須是一些東西! – 2011-01-31 18:55:49

0

您是否需要指定要在清單中接收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> 
相關問題