這是我第一次製作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活動,但現在我已經刪除了整個項目和清理了我自己的項目很多次。儘管如此,這項活動一再出現。 編輯 查克它我會很快重新開始......感謝所有的輸入。
我的建議有幫助嗎? –
Nopes dude ...試了一下... – adi928