tags:

views:

12

answers:

1

I have to call httpClient Within broadCastReciver ,but I got the following error Can u just help me.

 package com.BroadCast;

   import java.util.ArrayList;
   import java.util.List;

   import org.apache.http.NameValuePair;
   import org.apache.http.client.HttpClient;
   org.apache.http.client.methods.HttpPost;
   import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;

   import android.content.BroadcastReceiver;
   import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
   import android.os.Bundle;
 import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class SmsReciever extends BroadcastReceiver
{
public static final Uri uriSms = Uri.parse("content://sms");
 String str = ""; 
 String str1 = "5554"; 
 String str2 = ""; 
@Override
public void onReceive(Context context, Intent intent) { 
     if(!intent.getAction().equals 
             ("android.provider.Telephony.SMS_RECEIVED")) 
                                    { 
                                            return; 
                                    } 



    // ---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras(); 
    SmsMessage[] msgs = null; 
    int j = 1; 
    if (bundle != null) { 
        // ---retrieve the SMS message received--- 
        Object[] pdus = (Object[]) bundle.get("pdus"); 
        // Set<String> Bundle.keySet(); 

        msgs = new SmsMessage[pdus.length]; 
        // DeleteSMSFromInbox(context, msgs); 
        for (int i = 0; i < msgs.length; i++) { 
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
            str2 = msgs[i].getOriginatingAddress(); 
            HttpClient(str2);

            if (str2.equals(str1)) { 

                Toast.makeText(context, "In If", Toast.LENGTH_LONG).show(); 
                DeleteSMSFromInbox(context, msgs); 

            } else { 

                Toast.makeText( 
                        context, 
                        "One Message received from :" 
                                + msgs[i].getOriginatingAddress(), 
                        Toast.LENGTH_LONG).show(); 

            } 

        } 

    } 
} 

private void DeleteSMSFromInbox(Context context, SmsMessage[] msgs) { 
    try { 

        Thread.sleep(5000); 
        Cursor c = context.getContentResolver().query( 
                Uri.parse("content://sms"), null, null, null, 
                null); 

        c.moveToFirst(); 
// long thread_id = c.getLong(0); // int thread_id = c.getInt(1); 

        long thread_id = c.getLong(0);

        Uri deleteUri; 
        String s = Long.toString(thread_id); 

        deleteUri = Uri.withAppendedPath(uriSms, s); 

        c.close(); 

        int count = context.getContentResolver().delete(deleteUri, null, 
                null); 

        Toast.makeText( 
                context, 
                "Recent Message  " + thread_id + " Deleted and Count--" 
                        + count, Toast.LENGTH_LONG).show(); 

    } catch (Exception ex) { 

        Toast.makeText(context, "Error : " + ex, Toast.LENGTH_SHORT).show(); 
    } 
}
public Void HttpClient(String str)
{
     HttpClient httpclient = new DefaultHttpClient();   
     HttpPost httppost = new HttpPost("http://20.0.0.45:7777/WebApplication1/NewServlet");   

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);   
     nameValuePairs.add(new BasicNameValuePair("number", str2));   








    return null;

}

}

   -11 12:26:23.870: ERROR/AndroidRuntime(903): FATAL EXCEPTION: main
  -11 12:26:23.870: ERROR/AndroidRuntime(903): java.lang.RuntimeException: Unable to      
   start receiver Com.ContactProject.SmsReciever:        
   android.view.WindowManager$BadTokenException: Unable to add window -- token null is   
  not for an application
  10-11 12:26:23.870: ERROR/AndroidRuntime(903):     at              
  android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)

    10-11 12:26:23.870: ERROR/AndroidRuntime(903):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)

10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.os.Handler.dispatchMessage(Handler.java:99) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.os.Looper.loop(Looper.java:123) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.app.ActivityThread.main(ActivityThread.java:4627) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at java.lang.reflect.Method.invokeNative(Native Method) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at java.lang.reflect.Method.invoke(Method.java:521) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at dalvik.system.NativeStart.main(Native Method) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.view.ViewRoot.setView(ViewRoot.java:509) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.app.Dialog.show(Dialog.java:241) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at Com.ContactProject.SmsReciever.onReceive(SmsReciever.java:45) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810) 10-11 12:26:23.870: ERROR/AndroidRuntime(903): ... 10 more 10-11 12:26:23.916: DEBUG/MmsSmsProvider(132): getThreadId: create new thread_id for recipients [5554]

A: 

Definitely one thing is wrong. OnReceive() is not meant to contain demanding processing that takes some time. It should be used just to notice what arrived and start corresponding actions in your Service or Activity.

Error you get is because you are trying to do something that is not possible from the BroadcastReceiver Context.

I guess you have some Activity/Service accompanied with BroadcastReceiver. So, I would recommend to move task execution to that place.

Desiderio
I solved My Problem it worked Properly,
bindal