2012-09-23 47 views
1

這是我第一次製作Android應用程序。這是一個簡單的SMS監聽器,然後將SMS顯示爲Toast。無法在BroadcastReceiver中實例化接收器

public class SmsReciever extends BroadcastReceiver { 

String msgBody = null; 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECIEVED")){ 
     Bundle bundle = intent.getExtras(); 
     SmsMessage[] msg = null; 
     String msg_from; 
     if(bundle != null) 
     { 
      try{ 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
      msg = new SmsMessage[pdus.length]; 
      for(int i=0;i<msg.length;i++) 
      { 
       msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
       msg_from = msg[i].getOriginatingAddress(); 
       msgBody += msg[i].getMessageBody(); 
      } 
      }catch(Exception e) 
      { 
       Log.d("Exception caught",e.getMessage()); 
      } 
      Toast.makeText(context, msgBody, Toast.LENGTH_SHORT).show(); 
     } 

    } 
}} 

我收到以下錯誤:

java.lang.RuntimeException: Unable to instantiate receiver com.example.droid.SmsReceiver: java.lang.ClassNotFoundException: com.example.droid.SmsReceiver 

我向你保證SmsReciever.java是DROID/src目錄。我花了整晚研究這個問題。請幫忙。

編輯:這是您要求的清單xml文件。

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" android:permission="android.permission.RECEIVE_SMS"> 
    <activity 
     android:name=".JarvisActivity" 
     android:label="@string/title_activity_jarvis" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".SecureMessagesActivity" 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=".SmsReceiver" android:exported="false" > 
     <intent-filter android:priority="1000"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
</receiver> 
</application> 
</manifest> 

編輯:我做了一些研究,並發現,在我的清單文件我有一個指向SecureMessagesActivity活動代碼。但是在我的整個項目中,我沒有這個名字的活動。那麼這是問題嗎?

編輯:好吧,所以我從我的清單文件中刪除了SecureMessageActivity部分,我想它刪除了錯誤。現在,我得到這個Permisssion拒絕消息:

Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x10 (has extras) } to net.learn2develop.SMSMessaging requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001) 

而且在試圖複製另一個代碼爲同一目的,使用由我net.learn2develop.SMSMessaging活動,但現在我已經刪除了整個項目和清理了我自己的項目很多次。儘管如此,這項活動一再出現。 編輯 查克它我會很快重新開始......感謝所有的輸入。

+0

我的建議有幫助嗎? –

+0

Nopes dude ...試了一下... – adi928

回答

1

你有你的接收器在你的Adnroid清單中嗎?如果是,請提供AndroidManifest.xml。您可能還需要檢查以下相關問題:Android - SMS Broadcast receiver,Android SMS receiver not working,BroadcastReceiver + SMS_RECEIVED

編輯

嘗試添加android:exported="true"BroadcastReceiver。該接收機的工作對我來說:

<receiver android:name=".SmsBroadcastReceiver" 
      android:enabled="true" 
      android:exported="true"> 
    <intent-filter> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 

你現在有android:exported="false"基本上告訴Android你BroadcastReceiver不能從您的應用程序之外的來源接收消息。有關此標籤的更多信息,請訪問:http://developer.android.com/guide/topics/manifest/receiver-element.html

+0

嘿,我已經添加了清單文件。你可以再看一遍嗎? – adi928

+0

編輯我的答案。將android:導出的標籤更改爲「true」。它應該有所幫助。 –

+0

3歲,仍然完美。謝謝! –

0

試圖改變自己的接收器像這樣的清單:

<receiver android:name="com.example.droid.SmsReceiver" > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
</receiver> 
+0

仍然得到相同的錯誤.. :(...可以有我的Eclipse軟件或我安裝的SDK的問題...我真的很無奈這裏 – adi928

+0

我也實現了這樣的事情,無論你提供的代碼是完美的。嘗試通過eclipse清理你的項目,然後檢查。 –

0

我有想法,可以幫助你:讓我們嘗試此代碼:

<receiver android:name="com.example.droid.SmsReceiver" 
     android:exported="true" 
     android:enabled="true"> 
     <intent-filter android:priority="2147483647"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      <action android:name="android.provider.Telephony.GSM_SMS_RECEIVED" /> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED2" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     <intent-filter android:priority="2147483647"> 
      <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
      <data android:scheme="sms" /> 
      <data android:host="localhost" /> 
     </intent-filter> 
</receiver> 

不同類型的設備可以使用其自己的行動這不是標準的名稱。

相關問題