我正在开发一个包含IntentService的应用程序,以便在应用程序处于后台时向服务器发送请求,而服务器的响应将使用intent启动活动。问题是,当应用程序处于后台时,它会在没有任何用户交互的情况下自动打开活动。当应用程序到达前台时,如何避免这种仅打开的活动。
这是我的IntentService代码:
public class OTPIntentService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public CRegistrationSessionManagement m_oSessionManagement;
public String m_szMobileNumber, m_szEncryptedPassword;
public String TAG = "OTPIntentService";
public OTPIntentService() {
super("OTPIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final Context m_Context = getApplicationContext();// get activity context
m_oSessionManagement = new CRegistrationSessionManagement(m_Context);// creating object of Registartion session
// retreive user data from shared preferencce........
HashMap<String, String> user = m_oSessionManagement.getRegistrationDetails();// getting String from Regisatrtion session
m_szEncryptedPassword = user.get(CRegistrationSessionManagement.s_szKEY_PASSWORD).trim();// get password from registartion session
m_szMobileNumber = user.get(CRegistrationSessionManagement.s_szKEY_MOBILENUMBER).trim();// get mobile number from registartion session
try {
String json;
JSONObject jsonObject = new JSONObject();
jsonObject.put("agentCode", m_szMobileNumber);
jsonObject.put("pin", m_szEncryptedPassword);
jsonObject.put("otpCode", COTPVerificationDataStorage.getInstance().getM_szOtp());
json = jsonObject.toString();
System.out.println("Request:-" + json);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_szOtpverification, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("Response:-" + response);
try {
if (response.getString("resultDesc").equalsIgnoreCase("Transaction Successful")) {
} else if (response.getString("resultDesc").equalsIgnoreCase("OTP MisMatch")) {
if (NetworkUtil.isAppIsInBackground(m_Context)) {
} else {
Intent i = new Intent(m_Context, COtpManualVerificationScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
} else if (response.getString("resultDesc").equalsIgnoreCase("otpCode Can Not Be Empty")) {
if (NetworkUtil.isAppIsInBackground(m_Context)) {
} else {
Intent i = new Intent(m_Context, COtpManualVerificationScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error:-" + error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(m_Context);
requestQueue.add(jsonObjectRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
启动Activity
的{{1}}应创建Service
并听取响应。当调用BroadcastReceiver
中的onReceive()
时,它可以更新UI。如果应用程序在后台,则当用户将应用程序带到前台时,用户将看到更新的UI。如果在调用BroadcastReceiver
时应用程序已位于前台,则用户将立即看到UI更新。
在onReceive()
中,要将数据传回Service
,您需要使用Activity
而不是使用Intent
发送广播sendBroadcast()