tags:

views:

30

answers:

1
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 ?

A: 

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