我正在尝试将发件人的手机号码和通过短信收到的消息POST到网站。。没有错误,但应用程序仍然没有打开。你能建议一条路吗??
这是manifest.xml文件:
<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>
这是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) {
}
}
}
上下文:
[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!
首先,根据我所见,您实际上并不是在POST
中处理数据。看看这个,你应该能够弄清楚如何POST
。
其次,您需要从BroadcastReceiver
启动Service
,然后在Service
中执行POST
。当您在BroadcastReceiver
中调用它时,它可能没有时间完成请求。
第三,正如绣球花所提到的,您需要在Manifest
中声明您的接收器
首先:在清单中,接收器应该在应用程序标记中。