0
public class SMSWidget extends AppWidgetProvider {
private static final String queryString = "@inpion";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";


@Override
public void onReceive(Context context, Intent intent) {
    //get SMS Message
    if(intent.getAction().equals(SMS_RECEIVED)){            
        Bundle bundle = intent.getExtras();
        if(bundle != null){
            Object[] pdus = (Object[])bundle.get("pdus");   
            int pduslen = pdus.length;
            SmsMessage[] messages = new SmsMessage[pduslen];

            String str= "";
            for(int i=0; i<pduslen; i++){
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);

                try {
                    str += new String(messages[i].getMessageBody().getBytes(),"GB2312");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            for(SmsMessage message : messages){
                String msg = message.getMessageBody();
                Log.d("======messages=", messages.toString());
                if(msg.toLowerCase().startsWith(queryString)){
                    Log.d("======SMS RECIVED======", "OK");
                    //View new message
                    RemoteViews rv ;
                    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                    ComponentName smsWidget = new ComponentName( context, SMSWidget.class );                        
                    rv = new RemoteViews(context.getPackageName(), R.layout.main);
                    rv.setTextViewText(R.id.catch_sms, "msg:"+msg);
                    appWidgetManager.updateAppWidget(smsWidget, rv );                       


                    //Delete SMS
                    try{
                        Uri uriSms = Uri.parse("content://sms/inbox");
                        Cursor c = context.getContentResolver().query(uriSms, null, null, null, null);
                        if(c != null ){
                            //do{
                                int threadId = c.getInt(1);
                                Log.d("===threadId: ",threadId+"");
                                int a = context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
                                Log.d("===a: ", a+"");
                            //}while (c.moveToNext());
                        }else{

                            Log.d("=no=", c.toString());
                        }
                    }catch(Exception e){
                        e.printStackTrace();

}}}}


        android.xml :
         <application android:icon="@drawable/icon" android:label="@string/app_name">
         <receiver android:name=".SMSWidget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>            
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/catch_sms_widget_provider" />
    </receiver>

</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-sdk android:minSdkVersion="5" />

in my app,i send sms but in home screen ,AppWidget cannot receive it,only notification can notify me,i want to the message frist was received in my AppWidget not appear in notification bar,thank you ?

pengwang
  • 19,536
  • 34
  • 119
  • 168
  • 1
    You'll not be able to delete the message. I've noticed when the BroadcastReciever is called, the message hasn't been written into the database yet. So you're trying to delete something that hasn't been created it...you may wanna `sleep` for sometime before attempting to delete it. – st0le Sep 25 '10 at 11:24
  • 1
    http://stackoverflow.com/questions/1741628/can-we-delete-an-sms-in-android-before-it-reaches-the-inbox/2566199#2566199 – Christopher Orr Sep 25 '10 at 12:11
  • thank you,i can receive the message,but how to close the system's notification? – pengwang Sep 26 '10 at 03:59

1 Answers1

0

in my code msg.toLowerCase().startsWith(queryString) canot run ,so i delete it,at the same time in xml intent-filter android:priority="1" which i modify android:priority="20" and in the part "Delete SMS" i not realize temporary

pengwang
  • 19,536
  • 34
  • 119
  • 168