显示收到的消息android

时间:2014-11-23 21:52:24

标签: java android sms messages

我试图创建一个增加/减少数字的虚拟应用程序,但在后台它正在收听任何收到的消息,我无法在应用程序中显示任何收到的消息,继承我的smsReceiver类的代码。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;
import android.widget.Toast;

/* Class used to listen for incoming SMS Messages */
public class SMSReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String msg_from;
        String msgBody ="";
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                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]);
                   // msg_from = msgs[i].getOriginatingAddress();
                     msgBody = msgs[i].getMessageBody();//.toString();
                }
            }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
            }
        }
        try {
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
            File dir = new File (root.getAbsolutePath() + "/Bonbon info");
            dir.mkdirs();
            File f = new File(dir, "test.txt");
            FileWriter fw = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(msgBody);
            out.close();
            }
            } catch (IOException e) {
            }

        //---display the new SMS message---
        Toast.makeText(context, msgBody, Toast.LENGTH_LONG).show();

    }                      





}
}

我还将此添加到我的清单中以便接收。

  `<receiver android:name=".listener.SMSReceiver">
    <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
     </intent-filter>
    </receiver>`

和此权限

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

错误即将到来。

java.lang.RuntimeException:无法实例化接收器cps633.lab4.listener.SMSReceiver:java.lang.ClassNotFoundException:cps633.lab4.listener.SMSReceiver

0 个答案:

没有答案