-4

I am receiving sms using Broadcast Receiver.It is working fine. Now I want to read sms from inbox using service, (which is received by broadcast receiver).

I want to retreive sms from inbox using SERVICE. SMS retreival must be happen in background not in main thread.No any Activity should be used. //Broadcast receiver to receive sms and starting a service via intent public class SMSReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Bundle  bundle = intent.getExtras();
    SmsMessage[] message = null;
    String str = "";
    if(bundle != null){
        Object[] pdus = (Object[])bundle.get("pdus");
        message = new SmsMessage[pdus.length];
        for(int i = 0; i<message.length; i++){
            message[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            str += "New SMS from cloudy contacts " + message[i].getOriginatingAddress();
        }

        Toast.makeText(context, str, Toast.LENGTH_LONG).show();

        Intent intent1 = new Intent(context,MyService.class);
        context.startService(intent1);
    }

}

}

Servie public class MyService extends Service{ ReadSMS readSMS;

@Override
public IBinder onBind(Intent arg0) {



    return null;
}

public void onCreate(Bundle savedInstanceState){
    Log.d("Service","inside onCreate of service");


}

public void onDestroy(){
    Log.d("Service", "destroyed");

}

public void onStart(){
    Log.d("Service","starting service to read sms from inbox");
    Toast.makeText(this,"Reading sms from inbox",Toast.LENGTH_LONG).show();
    readSMS = new ReadSMS();
    ArrayList list = readSMS.readSms("inbox");

}

public class ReadSMS{



    public ArrayList readSms(String inbox){
        ArrayList sms = new ArrayList();
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor cursor = getContentResolver().query(uri, new String[]{"_id","address","date","body"},null,null,null);
        cursor.moveToLast();
        String address = cursor.getString(1);
        String body = cursor.getString(3);
        sms.add(address+" "+body);
        return sms;


    }
}

}

4

1 回答 1

0

The Messages in the inbox wouldn't be received by a broadcast receiver since it's not a broadcast. You will have to read the native database on the device.

Take a look here

EDIT: First of all, doing something from a Service doesn't automatically spawn a background thread. The service will run on the main thread.

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

Then you can of course spawn a thread yourself, or use an IntentService. But start by reading here.

于 2013-04-06T06:11:29.563 回答