来电时从 url 读取内容
read content from url when incoming call
我正在尝试找出如何在来电期间从 url 获取字符串,但它总是以错误 "Unfortunatelly stopped" 结束。我做错了什么?
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 {
Context c;
@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://androidexample.com/media/webservice/getPage.php";
// 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 = "My";
}
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.
Toast.makeText(c, "Idem na internet", Toast.LENGTH_LONG).show();
}
// 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]);
String serverURL = "http://androidexample.com/media/webservice/getPage.php"; //toto by tu nemuselo byt
HttpGet httpget = new HttpGet(serverURL);
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(c, "msg msg"+Content, Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Err:" +Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(c, "internet internet"+Content, Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Internet:" +Content, Toast.LENGTH_LONG).show();
}
}
}
}
我的日志文件:
03-04 13:01:09.381: E/(10486): Device driver API match
03-04 13:01:09.381: E/(10486): Device driver API version: 29
03-04 13:01:09.381: E/(10486): User space API version: 29
03-04 13:01:09.381: E/(10486): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
03-04 13:01:26.846: E/AndroidRuntime(10486): FATAL EXCEPTION: main
03-04 13:01:26.846: E/AndroidRuntime(10486): Process: com.example.detectincomingcall, PID: 10486
03-04 13:01:26.846: E/AndroidRuntime(10486): java.lang.RuntimeException: Unable to start receiver com.example.detectincomingcall.MyCallReceiver: java.lang.NullPointerException
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2675)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.access00(ActivityThread.java:175)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.Looper.loop(Looper.java:146)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.main(ActivityThread.java:5602)
03-04 13:01:26.846: E/AndroidRuntime(10486): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 13:01:26.846: E/AndroidRuntime(10486): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-04 13:01:26.846: E/AndroidRuntime(10486): at dalvik.system.NativeStart.main(Native Method)
03-04 13:01:26.846: E/AndroidRuntime(10486): Caused by: java.lang.NullPointerException
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.widget.Toast.<init>(Toast.java:115)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.widget.Toast.makeText(Toast.java:273)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.example.detectincomingcall.MyCallReceiver$LongOperation.onPreExecute(MyCallReceiver.java:68)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.AsyncTask.execute(AsyncTask.java:535)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.example.detectincomingcall.MyCallReceiver.onReceive(MyCallReceiver.java:37)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2660)
03-04 13:01:26.846: E/AndroidRuntime(10486): ... 10 more
我终于找到问题所在 - 我必须更改此设置:
// This code will execute when the phone has an incoming call
// Server Request URL
至此
// This code will execute when the phone has an incoming call
c = context;
// Server Request URL
感谢日志方面的帮助!
我正在尝试找出如何在来电期间从 url 获取字符串,但它总是以错误 "Unfortunatelly stopped" 结束。我做错了什么?
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 {
Context c;
@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://androidexample.com/media/webservice/getPage.php";
// 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 = "My";
}
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.
Toast.makeText(c, "Idem na internet", Toast.LENGTH_LONG).show();
}
// 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]);
String serverURL = "http://androidexample.com/media/webservice/getPage.php"; //toto by tu nemuselo byt
HttpGet httpget = new HttpGet(serverURL);
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(c, "msg msg"+Content, Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Err:" +Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(c, "internet internet"+Content, Toast.LENGTH_LONG).show();
//Toast.makeText(context, "Internet:" +Content, Toast.LENGTH_LONG).show();
}
}
}
}
我的日志文件:
03-04 13:01:09.381: E/(10486): Device driver API match
03-04 13:01:09.381: E/(10486): Device driver API version: 29
03-04 13:01:09.381: E/(10486): User space API version: 29
03-04 13:01:09.381: E/(10486): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
03-04 13:01:26.846: E/AndroidRuntime(10486): FATAL EXCEPTION: main
03-04 13:01:26.846: E/AndroidRuntime(10486): Process: com.example.detectincomingcall, PID: 10486
03-04 13:01:26.846: E/AndroidRuntime(10486): java.lang.RuntimeException: Unable to start receiver com.example.detectincomingcall.MyCallReceiver: java.lang.NullPointerException
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2675)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.access00(ActivityThread.java:175)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.Looper.loop(Looper.java:146)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.main(ActivityThread.java:5602)
03-04 13:01:26.846: E/AndroidRuntime(10486): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 13:01:26.846: E/AndroidRuntime(10486): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-04 13:01:26.846: E/AndroidRuntime(10486): at dalvik.system.NativeStart.main(Native Method)
03-04 13:01:26.846: E/AndroidRuntime(10486): Caused by: java.lang.NullPointerException
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.widget.Toast.<init>(Toast.java:115)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.widget.Toast.makeText(Toast.java:273)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.example.detectincomingcall.MyCallReceiver$LongOperation.onPreExecute(MyCallReceiver.java:68)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.os.AsyncTask.execute(AsyncTask.java:535)
03-04 13:01:26.846: E/AndroidRuntime(10486): at com.example.detectincomingcall.MyCallReceiver.onReceive(MyCallReceiver.java:37)
03-04 13:01:26.846: E/AndroidRuntime(10486): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2660)
03-04 13:01:26.846: E/AndroidRuntime(10486): ... 10 more
我终于找到问题所在 - 我必须更改此设置:
// This code will execute when the phone has an incoming call
// Server Request URL
至此
// This code will execute when the phone has an incoming call
c = context;
// Server Request URL
感谢日志方面的帮助!