2014-06-06 71 views
1

我的應用程序有點問題。我需要實例化傳入的SMS消息並將文本寫入文件。所以,我有無法實例化SMS消息java.lang.RunTimeExeption

package com.reciever; 
    public class SmsBroadcastReceiver extends BroadcastReceiver 
    { 
     private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      String newFolder = "/Budgetmate"; 
      String newFolder1="/logs"; 
      String sms_from=""; 
      if (intent != null && intent.getAction() != null && 
        ACTION.compareToIgnoreCase(intent.getAction()) == 0) { 
       Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); 
       SmsMessage[] messages = new SmsMessage[pduArray.length]; 
        for (int i = 0; i < pduArray.length; i++) { 
         messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]); 
        } 
       sms_from = messages[0].getDisplayOriginatingAddress(); 
       // if (sms_from.equalsIgnoreCase("900")) { 
        StringBuilder bodyText = new StringBuilder(); 
        for (int i = 0; i < messages.length; i++) { 
         bodyText.append(messages[i].getMessageBody()); 
        // } 
        String body = bodyText.toString(); 
        Intent mIntent = new Intent(context, SmsService.class); 
        mIntent.putExtra("sms_body", body); 
        context.startService(mIntent); 

       String dir = Environment.getExternalStorageDirectory().toString()+newFolder+newFolder1; 
       File logFile = new File(dir+File.separator+"sms_from.txt"); 
        if (!logFile.exists()) 
        { 
         try 
         { 
         logFile.createNewFile(); 
         } 
         catch (IOException e) 
         { 
         e.printStackTrace(); 
         } 
        } 
        try 
        { 
         BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
         buf.append(sms_from); 
         buf.newLine(); 
         buf.append(body); 
         buf.close(); 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
       abortBroadcast(); 
      } 
     } 
    } 
    } 

,這裏是我的清單

'<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.smsparser" 
android:versionCode="1" 
android:versionName="1.0" > 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="19" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 




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

    <activity 

     android:name="com.smsparser.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> 
</application> 

我把我的MainActivity和Reciever到不同的包,但它仍然無法正常工作,並拋出java.lang.RuntimeExeption。無法從com.smsparser java.lang.ClassNotFoundExeption實例化接收器。

你能幫我解決嗎?

+0

難道你還發布目錄下載? – User12111111

回答

1

變化<receiver android:name=".SmsBroadcastReceiver">

<receiver android:name="com.reciever.SmsBroadcastReceiver">

+0

謝謝!這非常有幫助。我必須說,如果你需要它的工作,你還應該把你的接收器放到包含這個結構的包裏[包含你的主要活動的名稱]。[一些新的名字]。 – user3006167

1

你把你的包名混淆了。

您的接收器位於com.reciever(順便提一句,拼寫錯誤),但您的清單告訴系統它在com.smsparseer.SmsBroadcastReceiver

您需要修復您的清單,或者更好地將您的Receiver移入與清單狀態相同的包中。

相關問題