I am trying to find out how to get string from url during incoming call, but it always ends with error "Unfortunatelly stopped". What am I doing wrong?
package com.example.detectincomingcall;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
//import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;
//import android.widget.TextView;
import android.widget.Toast;
public class MyCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// Server Request URL
String serverURL = "http://ift.tt/1E7O5Gi";
// Create Object and call AsyncTask execute Method
new LongOperation().execute(serverURL);
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
String mojText = "";
if (incomingNumber.equals("+"))
{
mojText = "aaa";
}
Toast.makeText(context, "Call from:" +incomingNumber+ " "+mojText, Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}
// Class with extends AsyncTask class
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private Context context;
private String Error = null;
protected void onPreExecute() {
// NOTE: You can call UI Element here.
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
try {
// Call long running operations here (perform background computation)
// NOTE: Don't call UI Element here.
// Server url call by GET method
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
// Close progress dialog
if (Error != null) {
Toast.makeText(context, "Call from:" +Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Call from:" +Content, Toast.LENGTH_LONG).show();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire