我已经制作了一个应用程序,它可以连续向服务器发送/接收lat。也可以从server.i检索和显示信息,并使用asynctask进行网络呼叫。但有一个问题,当电话来了,背景中的应用程序我的应用程序失去了连接;就像没有显示任何服务器信息,也没有发送lat长到服务器在正确的目的地。怎么解决这个? 就好像我会使用凌空,然后单例类可能有助于解决这个问题是否有任何asynctask的解决方案?
这是我的asynctask:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class
GetPostAsyncTask extends AsyncTask<String, Void, String> {
public AsyncResult asyncResult;
// HttpURLConnection httpURLConnection;
// private static String responseStr = "";
// private static String responseStrLogin = "";
ProgressDialog progressDialog;
private final String baseUrl = UserInfo.getBaseUrl();
Context context;
GetPostAsyncTask(Context context,AsyncResult asyncResult) {
this.context=context;
this.asyncResult= asyncResult;
}
@Override
protected void onPreExecute() {
//Toast.makeText(context,"Loading..",Toast.LENGTH_SHORT).show();
progressDialog=new ProgressDialog(context);
progressDialog.show();
}
@Override
protected String doInBackground(String... args) {
try {
// setting the URL
URL url = new URL(baseUrl+args[1]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.addRequestProperty("User-Agent", "RealTimeApps/1.0");
// setting the method type
httpURLConnection.setRequestMethod(args[0]);
// httpURLConnection.setChunkedStreamingMode(0);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
Log.v("Url",args[2]);
// setting the identification key name with query params
bufferedWriter.write(args[2]);
bufferedWriter.flush();
bufferedWriter.close();
Log.v("GetPostA", url.toString());
httpURLConnection.connect();
int getPostStatus = httpURLConnection.getResponseCode();
Log.v("GetPostSts", String.valueOf(getPostStatus));
String line = "";
String res = "";
// if(getPostStatus == 200){
// prepare the output buffer
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((line = bufferedReader.readLine()) != null) {
res += line;
}
inputStream.close();
// }
httpURLConnection.disconnect();
return res.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
Log.v("GetPostCatchMal",e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.v("GetPostCatchIOE", e.toString());
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
if(result!=null) {
asyncResult.asyncResult(result);
}
}
}
我是android的新手,无法找到这个问题的解决方案。请帮忙。 TIA
答案 0 :(得分:1)
您需要使用IncomingCallReceiver(用户定义:下面给出的代码)监听传入呼叫,并在接到呼叫时暂停/停止Asynctask,并在呼叫结束时恢复/重新启动它。
停止重启案例: 在您的Asynctask中,您可以在 sharedpreference 标志变量上循环以获得连续行为,并在调用到来时将标志设置为false。当呼叫结束时,您可以再次启动Asynctask。
IncomingCallReceiver的代码:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 2
this.context = context;
try{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
{
//Call incoming
}
else if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
{
//Call picked up
}
else if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))
{
//Call ends
}
}
catch(Exception e)
{
e.printStackTrace();
}
来电接收器教程: http://www.theappguruz.com/blog/detecting-incoming-phone-calls-in-android
共享偏好设置教程: https://www.tutorialspoint.com/android/android_shared_preferences.htm
Asynctask教程: https://developer.android.com/reference/android/os/AsyncTask.html
Singleton课程教程: https://sourcemaking.com/design_patterns/singleton
答案 1 :(得分:1)
您可以在Service中运行该代码。该服务可以在后台运行(即使应用程序暂停/停止) - 这意味着即使拨打电话(使您的活动处于暂停状态),您的代码也应该在没有中断的情况下运行。
请务必阅读有关服务生命周期的更多信息,以实现您正在尝试正确执行的操作。
编辑:
要在服务中运行AsyncTask,请在创建服务时创建该任务的实例(请参阅服务lifecycle)并在那里运行任务。
当您尝试启动已在运行的服务时,它将不会创建另一个实例,而是返回正在运行的服务的实例,但它将再次调用onStartCommand。
您可以bind您的活动服务与之通信。
答案 2 :(得分:0)
1.
<service
android:name=".AsyncTaskInServiceService">
</service>
2.
public class ServiceReciever extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : " + incomingNumber);
Toast.makeText(context,incomingNumber,Toast.LENGTH_LONG).show();
Asynchtask().execute();//Write the code here
}
},PhoneStateListener.LISTEN_CALL_STATE);
}