我的應用程序有點問題。我需要實例化傳入的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實例化接收器。
你能幫我解決嗎?
難道你還發布目錄下載? – User12111111