-1

I am trying to POST sender's mobile number and message received via SMS to a website..there are no errors but still the app doesn't open. Can you please suggest a way??

This is the manifest.xml file:

<uses-sdk android:minSdkVersion="8" />
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".SmsReceiveActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

This is the SMSReceive.java:

package com.wissen.sms.receiver;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

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

public class SMSReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
String mobile = PhoneNumberUtils.getNumberFromIntent(intent, context);
// show first message
Toast toast = Toast.makeText(context,
"Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
    try {
    // Construct data
    String xyz = smsMessage[0].getMessageBody();    
    String data = URLEncoder.encode("test", "UTF-8") + "=" + URLEncoder.encode(xyz,       "UTF-8");
    data = data + "&" + URLEncoder.encode("mobile", "UTF-8") + "=" +    URLEncoder.encode(mobile, "UTF-8");

    //   http://www.medibeep.in/xyz.php?test=123&mobile=1234556
    // Send data
    URL url = new URL("http://www.medibeep.in/xyz.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new             BufferedReader(newInputStreamReader(conn.getInputStream()));
    String line1;
    while ((line1 = rd.readLine()) != null) {
        System.out.println(line1);
    }
    wr.close();
    rd.close();
    } catch (Exception e) {
    }
}
}

Context:

[2012-02-02 22:28:19 - smsReceive] Android Launch!
[2012-02-02 22:28:19 - smsReceive] adb is running normally.
[2012-02-02 22:28:19 - smsReceive] Performing com.wissen.sms.receiver.SmsReceiveActivity activity launch
[2012-02-02 22:28:19 - smsReceive] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2012-02-02 22:28:24 - smsReceive] Uploading smsReceive.apk onto device 'emulator-5554'
[2012-02-02 22:28:24 - smsReceive] Installing smsReceive.apk...
[2012-02-02 22:28:31 - smsReceive] Failed to install smsReceive.apk on device 'emulator-5554!
[2012-02-02 22:28:31 - smsReceive] (null)
[2012-02-02 22:28:32 - smsReceive] Launch canceled!
[2012-02-02 22:30:27 - smsReceive] Failed to install smsReceive.apk on device 'emulator-5554!
[2012-02-02 22:30:27 - smsReceive] (null)
[2012-02-02 22:30:27 - smsReceive] Launch canceled!
Frohnzie
  • 3,559
  • 1
  • 21
  • 24
user1185741
  • 9
  • 1
  • 4

2 Answers2

1

First, you're not actually POSTing your data from what I can see. Look at this and you should be able to figure out how to POST.

Secondly, you need to start a Service from your BroadcastReceiver and then do the POST in the Service. When you call it in the BroadcastReceiver, it likely won't have time to complete the request.

Thirdly, as Hydrangea mentioned, you need to declare your receiver in the Manifest

Community
  • 1
  • 1
Reed
  • 14,703
  • 8
  • 66
  • 110
  • can you tell how to start service from broadcastreceiver ? – user1185741 Apr 07 '12 at 18:21
  • You would create an explicit intent for the `Service` and call `startService(intent)`. If you don't know how to do all this, I would suggest reading more of http://developer.android.com/guide/index.html – Reed Apr 08 '12 at 05:57
0

To start with: in your manifest, the receiver should be inside the application tags.

Turnsole
  • 3,422
  • 5
  • 30
  • 52