2013-05-03 29 views
0

嗨:)我正在用短信在android。我有問題與我的短信接收器類。當我第一次在模擬器上運行我的應用程序時,它可以正常工作,因爲我已將它編程爲工作。但是當我每次運行應用程序時,它都不起作用,因爲我更新了它的工作。我被困在最後2天。有人可以指導我或提供一些幫助。我的基本的接收機類是:我更新的短信接收器不工作Android

public class SMSreceiver extends BroadcastReceiver { 

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

    Bundle bundle = intent.getExtras(); 
    String Sender = null; 
    String str = ""; 
    SmsMessage [] msgs = null; 
    if(bundle != null) 
    { 
    Object[] pdus = (Object[]) bundle.get("pdus"); 
    msgs = new SmsMessage[pdus.length]; 

     for(int i=0;i<msgs.length;i++) 
     { 

      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
      Sender = msgs[i].getOriginatingAddress(); 
      str = "SMS From: " + msgs[i].getOriginatingAddress(); 
      str += ":"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n"; 
     } 
     //Toast.makeText(context, str, Toast.LENGTH_LONG).show(); 
     Toast.makeText(context, Sender, Toast.LENGTH_LONG).show(); 
    } 

} 

在上面的代碼中,我曾經評論,顯示了味精敬酒,並試圖顯示,顯示發送者的號碼敬酒。但它仍然顯示新的味精文本。這很奇怪。

這裏是我的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.pingpongsmsremote" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="12" /> 
    <uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.RECEIVE_SMS"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.pingpongsmsremote.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SMSScheduler" 
     android:label="@string/title_activity_smsscheduler" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.FilterSMS" 
     android:label="@string/title_activity_filter_sms" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SMSRemote" 
     android:label="@string/title_activity_smsremote" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SendSms" 
     android:label="@string/title_activity_send_sms" > 
    </activity> 
</application> 



</manifest> 
+0

你能告訴你在哪裏註冊的接收器?它是否在清單文件或活動中註冊? – akhalsa 2013-05-03 13:52:10

+0

@akhalsa是的,我gona編輯與清單我的問題文本。看看它 – Zeeshan 2013-05-03 13:56:49

+0

@akhalsa我編輯我的問題與清單文件。你可以現在看看它,並告訴問題在哪裏?謝謝:) – Zeeshan 2013-05-03 14:02:37

回答

2

看起來也許你忘了註冊接收?你需要這樣的線在你的清單:

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

要看到它在一個完整的清單例子是這樣的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.pingpongsmsremote" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="12" /> 
    <uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.RECEIVE_SMS"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.pingpongsmsremote.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SMSScheduler" 
     android:label="@string/title_activity_smsscheduler" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.FilterSMS" 
     android:label="@string/title_activity_filter_sms" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SMSRemote" 
     android:label="@string/title_activity_smsremote" > 
    </activity> 
    <activity 
     android:name="com.example.pingpongsmsremote.SendSms" 
     android:label="@string/title_activity_send_sms" > 
    </activity> 
    <receiver android:name="SMSreceiver" > 
       <intent-filter> 
        <action android:name="android.provider.telephony.SMS_RECEIVED"/> 
       </intent-filter> 
    </receiver> 
</application> 
</manifest> 
+0

,除非你正在註冊活動的某個地方? – akhalsa 2013-05-03 14:08:50

+0

沒問題。我相信我在我的清單中錯過了一些東西。但我不知道在哪裏添加這個?而且,沒有名爲「receiver」的標籤存在:| .. plz幫助我 – Zeeshan 2013-05-03 21:51:20

+0

看看本教程中的清單:http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html – akhalsa 2013-05-03 22:03:06