This question already has an answer here:
I know this question has been asked before on this site but i can't use that answer as i am running the app on mobile. As it is related to calling i have to test the app live on my phone.I am making an app that sends the incoming call number to my server and the app receives the output and shows it. But the app stops working after 2-3 seconds when i receives a call, it gives me a error that unfortunately app stopped working. call.java
package com.example.hudixt.trigger;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class call extends BroadcastReceiver {
private Context mContext;
private Intent mIntent;
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ift.tt/1z4RXH9");
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
break;
case TelephonyManager.CALL_STATE_RINGING:
// -- check international call or not.
String MyName = incomingNumber; //any data to send
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
}
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = null;
try {
response = httpclient.execute(httppost, responseHandler);
Toast.makeText(mContext, response, Toast.LENGTH_LONG).show();
} catch (IOException e) {
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
}
Android Manifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.example.hudixt.trigger" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".call"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
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>
Please help me as i am beginner to android. Thanks in advance
Aucun commentaire:
Enregistrer un commentaire