This is my AsyncTask that is being executed when I click an item in a ListView. I display a ProgressDialog while the task is running and dismiss it at the end of onPostExecute() as shown below. This works and the spinner displays in my Nexus 5X emulator. On any other emulator or my real device (Galaxy S7 Edge Nougat) however, the dialog shows but without a spinner or with the spinner but it instantly freezes. I HAVE turned on all transitions/animations to 1x in the developer options by the way and all my other animations work so I know that is not the issue.
public class LoadHunt extends AsyncTask<String, Void, String> {
private static final String TAG = LoadHunt.class.getSimpleName();
private PostLoginFragment listener;
private String hunt;
private int item;
private ProgressDialog pd;
public LoadHunt(PostLoginFragment listener, int item) {
this.listener = listener;
this.item = item;
}
#Override
public void onPreExecute() {
pd = new ProgressDialog(listener.getActivity());
pd.setMessage("Loading...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.show();
//((PostLoginFragment)listener).getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
#Override
protected String doInBackground(String... huntname) {
try {
hunt = huntname[0];
String link = "https://cniekirk.com/gethunt3.php?huntname=" + huntname[0];
URL url = new URL(link);
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
urlCon.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
StringBuffer sb = new StringBuffer("");
String s = "";
while((s = in.readLine()) != null){
sb.append(s);
}
in.close();
return sb.toString();
}catch (Exception e) {
Log.e(TAG, "Connection error!");
return null;
}
}
#Override
protected void onPostExecute(String result){
List<String> resultSplit = Arrays.asList(result.substring(0, result.length() - 5).split(" xxxx"));
List<List<String>> clueDetailsSplit = new ArrayList<>();
for(String clueStuff: resultSplit) {
clueDetailsSplit.add(Arrays.asList(clueStuff.split("\\s+")));
}
final List<Clue> clues = new ArrayList<>();
for(List<String> clueDetails : clueDetailsSplit) {
final StringBuilder stringBuilder = new StringBuilder();
for(int i = 1; i <= clueDetails.size() - 5; i++) {
if(stringBuilder.length() > 0) {
stringBuilder.append(" ");
}
stringBuilder.append(clueDetails.get(i));
}
final Clue clue;
if(!(clueDetails.get(clueDetails.size() - 1).equals("noimage"))) {
clue = new Clue(clueDetails.get(0), stringBuilder.toString(), com.ploetz.dev.treasurehunt.Data.Status.LOCKED,
Double.parseDouble(clueDetails.get(clueDetails.size() - 3)), Double.parseDouble(clueDetails.get(clueDetails.size() - 2)),
getBitmapFromUrl("https://cniekirk.com/" + clueDetails.get(clueDetails.size() - 1)));
} else {
clue = new Clue(clueDetails.get(0), stringBuilder.toString(), com.ploetz.dev.treasurehunt.Data.Status.LOCKED,
Double.parseDouble(clueDetails.get(clueDetails.size() - 3)), Double.parseDouble(clueDetails.get(clueDetails.size() - 2)));
}
clues.add(clue);
}
//((PostLoginFragment)listener).getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
GlobalResources.setHuntClues(clues);
GlobalResources.setHuntname(hunt);
GlobalResources.organiseClues();
GlobalResources.setItemClicked(item);
//System.gc();
pd.dismiss();
listener.onTaskCompleted();
}
public Bitmap getBitmapFromUrl(final String src) {
final CountDownLatch latch = new CountDownLatch(1);
final Bitmap[] bmp = new Bitmap[1];
Thread thread = new Thread(){
#Override
public void run(){
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bmp[0] = BitmapFactory.decodeStream(input, null, options);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
// Causes latch.await() to stop blocking and carry on execution
latch.countDown();
}
};
thread.start();
try {
latch.await();
}catch (InterruptedException e) {
Log.e(TAG, "Thread error!");
}
return bmp[0];
}
}
Related
I have integrated a webservice and after integrating it goes to the next page.
But the problem is while navigating to from login page to next page, it is showing blank screen and I do not understand what's the problem.
Kindly Help!!
Thanks in advance.
Here is my code
public class Mobile_Number_Activity extends Activity {
private EditText ed_Mobile_Number;
private Button btnSubmit;
private TextView Hyperlink;
private ProgressDialog pDialog;
private JSONObject json;
private int success = 0;
private String path = "xxx";
private String strMobileNumber = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mobile_number);
ed_Mobile_Number = (EditText) findViewById(R.id.ed_Mobile_Number);
btnSubmit = (Button) findViewById(R.id.btn_mobile_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (!ed_Mobile_Number.getText().toString().equals(""))
{
strMobileNumber = ed_Mobile_Number.getText().toString();
new SendPostRequest(getApplicationContext()).execute(strMobileNumber);
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Mobile Number", Toast.LENGTH_LONG).show();
}
}
});
}
Here is SendPost Request Class which extends Asyntask
public class SendPostRequest extends AsyncTask<String, Void, String> {
public static ProgressDialog pDialog;
private String path = "xxxx";
public String Mobile_Number = "";
private Context mContext;
public SendPostRequest(Context context)
{
this.mContext=context;
pDialog = new ProgressDialog(mContext);
pDialog.setTitle("Loading...");
pDialog.setMessage("Please wait...");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
#Override
protected String doInBackground(String... arg0)
{
try
{
Thread.sleep(40000);
String m_number=arg0[0];
URL url = new URL(path); // here is your URL path
JSONObject postDataParams = new JSONObject();
postDataParams.put("username", "data");
postDataParams.put("password", "data");
postDataParams.put("mobile", m_number);
System.out.println(m_number);
Mobile_Number=m_number;
Log.e("params", postDataParams.toString());
//conversion of 15 min to miliseconds :900000
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setReadTimeout(1500/* milliseconds */);
//conn.setConnectTimeout(1500 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
} else {
return new String("false : " + responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
#Override
protected void onPostExecute(String result) {
if (pDialog != null && pDialog.isShowing()) {
//Toast.makeText(mContext.getApplicationContext(), result,Toast.LENGTH_LONG).show();
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result);
String otp = jsonObject.optString("otp");
Log.d("otp value is", otp);
message(otp);
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while (itr.hasNext()) {
String key = itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
public void message(String otp)
{
Intent myIntent = new Intent(this.mContext,OTP_Code_Activity.class);
myIntent.putExtra("Mobile Number", Mobile_Number);
System.out.println(Mobile_Number);
myIntent.putExtra("OTP Code", otp);
mContext.startActivity(myIntent);
pDialog.dismiss();
}
new SendPostRequest(getApplicationContext()).execute(strMobileNumber);
Change this to activity context.
new SendPostRequest(Mobile_Number_Activity.this).execute(strMobileNumber);
Delete(uninstall) the app from your phone, and run it again..It happens due to some issues with database. Or else I think you haven't mention the activity in the Manifest
Change
new SendPostRequest(getApplicationContext()).execute(strMobileNumber);
To
new SendPostRequest(this).execute(strMobileNumber);
And with Application Context, you can't show Dialog. You need to use Activity instance.
Change
SendPostRequest(Context context)
To
SendPostRequest(Activity context)
It might be helpful. Check log and see have any error in JSON Parse Exception as well.
How is it possible to change my asyncTask into service because everytime i close the app or the restart the device my aysncTask not working .
I have aysnc that sends Post request to php server and i got back an image on every request so i putted setting option to the user he can choose to the picture of wallpaper to change on every 1,5,10,..... min but like i said i want to be service so when the user choose the timing to change the wallpaper and closes (destroy) the application the server still countinue to change here is my code
public class MainActivity extends AppCompatActivity {
TextView txt;
Button btn;
String forecastJsonStr;
RadioButton rd1, rd2, rd3, rd4, rd5, rd6, rd7;
Handler mHandler;
RadioGroup radioGroup;
private final static int INTERVAL = 1000*60 * 1; //1 min
private final static int INTERVAL2 = 1000*60*5; // 5 min
private final static int INTERVAL3 = 1000 * 60 * 10; // 10 min
private final static int INTERVAL4 = 1000 * 60 * 15; // 15 min
private final static int INTERVAL5 = 1000 * 60 * 30; // 30 min
private final static int INTERVAL6 = 1000 * 60 * 60; // 1 hour
private final static int INTERVAL7 = 1000 * 60 * 1440; // 1 day
private final String hostName = "http://555.555.555.555";
private final String hostRequestName = "/yay.php";
private final String hostWallpaperName = "/wallpaper/";
private static int SELECTED_INTERVAL = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
rd1 = (RadioButton) findViewById(R.id.radioButton);
rd2 = (RadioButton) findViewById(R.id.radioButton2);
rd3 = (RadioButton) findViewById(R.id.radioButton3);
rd4 = (RadioButton) findViewById(R.id.radioButton4);
rd5 = (RadioButton) findViewById(R.id.radioButton5);
rd6 = (RadioButton) findViewById(R.id.radioButton6);
rd7 = (RadioButton) findViewById(R.id.radioButton7);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
mHandler = new Handler();
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(rd1.isChecked()) {
SELECTED_INTERVAL = INTERVAL;
} else if (rd2.isChecked()) {
SELECTED_INTERVAL = INTERVAL2;
}
startRepeatingTask();
}
});
}
void startRepeatingTask() {
mHandlerTask.run();
}
Runnable mHandlerTask = new Runnable() {
#Override
public void run() {
new WallpaperData().execute();
mHandler.postDelayed(mHandlerTask, SELECTED_INTERVAL);
}
};
private class WallpaperData extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("http://555.555.555.555/yay.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.write("method=get_random_wallpaper".getBytes());
wr.flush();
wr.close();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
Log.d("hey", buffer.toString());
}
if (buffer.length() == 0) {
return null;
}
forecastJsonStr = buffer.toString();
return forecastJsonStr;
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
#Override
protected void onPostExecute(final String forecastJsonStr) {
txt.setText(forecastJsonStr);
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap result = Picasso.with(getBaseContext())
.load(hostName + hostWallpaperName + forecastJsonStr)
.get();
wallpaperManager.setBitmap(result);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
thread.start();
super.onPostExecute(forecastJsonStr);
}
}
}
For time intervals larger than 30 secs you should use AlarmManager instead of Handler's .postDelayed method. See these tutorials: https://developer.android.com/training/scheduling/alarms.html, http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/ . There is a JonScheduler for scheduling repeating tasks, Vogella has tutorial on it: http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html .
In a few words: you create Service (InteneService), create PendingIntent, pointing to that service and schedule AlarmManager, which will be sending that intent, which, in turn, sill be launcing your IntentService.
I have an Async task that loads information from the server and displays data on the UI. Suddenly the async task downloads the data and formats the JSON data fine but it would freeze the UI completely.
Here is the base download class
public class GetRawData {
private static String LOG_TAG = GetRawData.class.getSimpleName();
private String mRawURL;
private List<NameValuePair> mRawParams = null;
private String mRawData;
private DownloadStatus mDownloadStatus;
public GetRawData(String mRawURL) {
this.mRawURL = mRawURL;
this.mRawParams = null;
this.mDownloadStatus = DownloadStatus.IDLE;
}
public String getRawData() {
return mRawData;
}
public void setRawURL(String mRawURL) {
this.mRawURL = mRawURL;
}
public List<NameValuePair> getRawParams() {
return mRawParams;
}
public void setRawParams(List<NameValuePair> mParams) {
this.mRawParams = mParams;
}
public DownloadStatus getDownloadStatus() {
return mDownloadStatus;
}
public void reset() {
this.mRawURL = null;
this.mRawData = null;
this.mDownloadStatus = DownloadStatus.IDLE;
}
public void execute() {
this.mDownloadStatus = DownloadStatus.PROCESSING;
DownloadRawData mDownloadRawData = new DownloadRawData();
mDownloadRawData.execute(mRawURL);
}
public class DownloadRawData extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
// Create URL and Reader instances.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
//If no parameter has been provided, return null.
if (params == null)
return null;
try {
// Get URL entered by the user.
URL mURL = new URL(params[0]);
urlConnection = (HttpURLConnection) mURL.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setRequestProperty("Content-Type","application/json");
//urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
urlConnection.connect();
// validate and add parameters if available.
if (mRawParams != null && mRawParams.size()>0){
JSONObject jsonParam = new JSONObject();
for (NameValuePair pair : mRawParams) {
jsonParam.put(pair.getName().toString(), pair.getValue().toString());
}
String jsonparams = jsonParam.toString();
// Send POST output.
DataOutputStream printout;
printout = new DataOutputStream(urlConnection.getOutputStream());
printout.writeBytes(jsonparams);
printout.flush();
printout.close();
}
int HttpResult =urlConnection.getResponseCode();
StringBuffer buffer = new StringBuffer();
if(HttpResult ==HttpURLConnection.HTTP_OK){
InputStream inputStream = urlConnection.getInputStream();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
System.out.println(""+buffer.toString());
}else{
InputStream errorStream = urlConnection.getErrorStream();
if (errorStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(errorStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
System.out.println(urlConnection.getResponseMessage());
}
return buffer.toString();
} catch (IOException e) {
Log.d("IOException", e.toString());
return null;
} catch (JSONException j) {
Log.d("JSONException", j.toString());
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.d("IOException", "unable to close the reader");
}
}
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mRawData = result;
//Log.d("onPostExecute", result);
if (mRawData == null) {
if (mRawURL == null) {
mDownloadStatus = DownloadStatus.NOT_INITIALIZED;
} else {
mDownloadStatus = DownloadStatus.FAILED_OR_EMPTY;
}
} else {
mDownloadStatus = DownloadStatus.PROCESSED;
}
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
}
}
enum DownloadStatus {
IDLE,
PROCESSING,
NOT_INITIALIZED,
FAILED_OR_EMPTY,
PROCESSED
}
Here is the specific data formatting class the extends above class
public class GetJobCardJsonData extends GetRawData {
private static String LOG_TAG = GetAuthenticationJsonData.class.getSimpleName();
private static String JOBCARD_SERVICE_URL = "http://www.appservice.com/appservice/jobcardinfoservice.asmx/GetJobCardInfo";
private List<JobCard> mJobCardList;
private CarcalDownloadListener mListener;
public GetJobCardJsonData(String CurrentDate, int DealershipID) {
super(null);
List<NameValuePair> mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("JobCardDate", CurrentDate));
mParams.add(new BasicNameValuePair("DealershipID", String.valueOf(DealershipID)));
this.setRawParams(mParams);
}
public List<JobCard> getJobCardList() {
return mJobCardList;
}
public void getjobcards() {
super.setRawURL(JOBCARD_SERVICE_URL);
DownloadJobCardJsonData mDownloadJobCardJsonData = new DownloadJobCardJsonData();
mDownloadJobCardJsonData.execute(JOBCARD_SERVICE_URL);
}
public void setOnCarcalDownloadListener(CarcalDownloadListener onCarcalDownloadListener) {
this.mListener = onCarcalDownloadListener;
}
private void processResult() {
if (getDownloadStatus() != DownloadStatus.PROCESSED) {
Log.e(LOG_TAG, "Error Downloading the raw file.");
return;
}
if (mJobCardList == null){
mJobCardList = new ArrayList<JobCard>();
}
final String JOBCARD_JOBCARDID = "JobCardID";
final String JOBCARD_GETSTOCKNUMBER_WITH_DELIVERYTIME = "StockNumberWithDeliveryTime";
final String JOBCARD_CUSTOMERNAME = "CustomerName";
final String JOBCARD_MODELNUMBER = "ModelNumber";
final String JOBCARD_COLOR = "Color";
final String JOBCARD_SALEEXECUTIVE = "SaleExecutive";
final String JOBCARD_ORDERSTATUS = "OrderStatus";
final String JOBCARD_SHOWROOMSTATUS = "ShowRoomStatus";
try {
JSONArray jsonArray = new JSONArray(getRawData());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jobcarditem = jsonArray.optJSONObject(i);
Long JOBCARDID = jobcarditem.getLong(JOBCARD_JOBCARDID);
String STOCKWITHDELIVERY = jobcarditem.getString(JOBCARD_GETSTOCKNUMBER_WITH_DELIVERYTIME);
String CUSTOMERNAME = jobcarditem.getString(JOBCARD_CUSTOMERNAME);
String MODELNUMBER = jobcarditem.getString(JOBCARD_MODELNUMBER);
String COLOR = jobcarditem.getString(JOBCARD_COLOR);
String SALEEXECUTIVE = jobcarditem.getString(JOBCARD_SALEEXECUTIVE);
int ORDERSTATUS = jobcarditem.getInt(JOBCARD_ORDERSTATUS);
int SHOWROOMSTATUS = jobcarditem.getInt(JOBCARD_SHOWROOMSTATUS);
JobCard mJobCard = new JobCard(JOBCARDID, STOCKWITHDELIVERY, CUSTOMERNAME, MODELNUMBER, COLOR, SALEEXECUTIVE, ORDERSTATUS, SHOWROOMSTATUS);
mJobCardList.add(mJobCard);
}
} catch (JSONException jsone) {
jsone.printStackTrace();
Log.e(LOG_TAG, "Error processing json data.");
}
}
public class DownloadJobCardJsonData extends DownloadRawData {
#Override
protected String doInBackground(String... params) {
return super.doInBackground(params[0]);
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
processResult();
mListener.OnDownloadCompleted();
}
}
}
Here is the code that is called on the activity
private JobCardRecyclerViewAdapter mJobCardRecyclerViewAdapter;
private GetJobCardJsonData mGetJobCardJsonData;
SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_card_calender);
activateToolbarWithHomeEnabled();
String formattedDate="";
if (session.getCurrentDate() == ""){
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = df.format(c.getTime());
currentDateTextView.setText(formattedDate);
}else {
formattedDate = session.getCurrentDate();
currentDateTextView.setText(formattedDate);
}
// Fetch data for current date.
mGetJobCardJsonData = new GetJobCardJsonData(formattedDate, session.getDealershipID());
mGetJobCardJsonData.getjobcards();
mGetJobCardJsonData.setOnCarcalDownloadListener(new CarcalDownloadListener() {
#Override
public void OnDownloadCompleted() {
List<JobCard> mJobCards = mGetJobCardJsonData.getJobCardList();
mJobCardRecyclerViewAdapter = new JobCardRecyclerViewAdapter(mJobCards, JobCardCalenderActivity.this);
mRecyclerView.setAdapter(mJobCardRecyclerViewAdapter);
}
});
}
Can anyone help on what i am doing wrong that is freezing the UI. It was working fine before and has started to freeze the UI suddenly.
I was able to fix the issue, the problem was not with Async task but with the layout. I accidently wrapped the recycler view with scroll view. which was causing the UI to freeze. Looks weird that a scroll view caused the whole UI thread to freeze. but here is my solution
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:id="#+id/jobCardRecyclerView"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/jobCardHeader"
android:scrollbars="vertical"></view>
</ScrollView>
Changed it to
<view
android:id="#+id/jobCardRecyclerView"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/jobCardHeader"
android:scrollbars="vertical"></view>
hope it will be helpful for others facing same problem.
I have a process in a button (large process). I want to update the TextView with diferents values. Like:
Connecting...
Receiving Files...
Complete...
etc..
I'm using setText(); void, but I'm getting only the last value of setText();, in that case:
estatEdit.setText("Tareas completadas!");
Here is my code...
public class sincroGeneral extends Activity implements OnClickListener {
private static final String CATEGORIA = "libro";
private static final String HOST = "192.168.1.165";
private static final int PORT = 9889;
private static final int PORTDATOS = 9888;
int filesize=6022386;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [filesize];
byte buffer[] = new byte[1024];
byte buffer2[] = new byte[1024];
byte bufferArxiu[] = new byte[2048];
int s;
int s2;
String Benvinguda;
String compra;
EditText estatEdit;
EditText editInflated;
File myfile;
SQLiteDatabase baseDatos;
LinearLayout mainLayout;
View view2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.sincrolayout);
Button b = (Button) findViewById(R.id.BotoEnviarSincro);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button) findViewById(R.id.BotoEnviarSincro);
estatEdit = (EditText) findViewById(R.id.editSincro);
estatEdit.setText("Enviando peticiĆ³n...");
b.setClickable(false);
b.setFocusable(false);
b.setEnabled(false);
ProcesRebre();
b.setClickable(true);
b.setFocusable(true);
b.setEnabled(true);
}
});
}
private void mostrarMensaje(String mensaje)
{
Toast.makeText(this, mensaje, 500).show();
}
private void ProcesRebre()
{
Socket sockDatos = null;
Socket clientSocket = null;
DataOutputStream sortida;
DataInputStream entrada;
BufferedReader inFromServer;
DataOutputStream sortidaDatos;
DataInputStream entradaDatos;
BufferedReader inFromServerDatos;
try {
estatEdit.setText("Conectando...");
clientSocket = new Socket(HOST, PORT);
sortida = new DataOutputStream(clientSocket.getOutputStream());
entrada = new DataInputStream(clientSocket.getInputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sortida.writeBytes("FAS4-F#S-1-F#S- 1.0.152-F#S-");
Log.i("Enviat","OK");
clientSocket.setSoTimeout(30000);
s = entrada.read(buffer);
String str = new String(buffer, "UTF8");
clientSocket.setSoTimeout(0);
Log.i("Rebut","OK");
if(str.contains("PDA1-F#S-"))
{
sockDatos = new Socket(HOST, PORTDATOS);
estatEdit.setText("Esperando archivo....");
sortidaDatos = new DataOutputStream(sockDatos.getOutputStream());
entradaDatos = new DataInputStream(sockDatos.getInputStream());
inFromServerDatos = new BufferedReader(new InputStreamReader(sockDatos.getInputStream()));
sortidaDatos.writeBytes("FAS4-F#S-1-F#S- 1.0.150-F#S-");
if(sockDatos.isConnected())
{
System.out.println("Conectat amb port 9888");
}
File carpetaSincro = new File(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/");
carpetaSincro.mkdirs();
File ArxiuSincro = new File (Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/sincroorders.zip");
if(ArxiuSincro.exists())
{
ArxiuSincro.delete();
}
File ArxiuSincro2 = new File (Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/sincroorders.zip");
ArxiuSincro2.createNewFile();
sortida.writeBytes("FAS2-F#S-");
String str2= "";
clientSocket.setSoTimeout(30000);
while(true && (!str2.contains("PDA7-F#S-") && !str2.contains("PDAERR1-F#S-")))
{
s2 = entrada.read(buffer2);
str2 = new String(buffer2, "UTF8");
}
clientSocket.setSoTimeout(0);
String replace1 = str2.replace("PDA7-F#S-", "");
String replace2 = replace1.replace(" ", "");
String tamanyArxiu = replace2.replace("-F#S-ZIP","");
int bufferZip = Integer.parseInt(tamanyArxiu);
boolean in;
s2 = 0;
sockDatos.setSoTimeout(30000);
RandomAccessFile archivo = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/sincroorders.zip", "rw");
long tamArxActual = 0;
while(bufferZip>=tamArxActual)
{
sockDatos.setSoTimeout(10000);
s2 = entradaDatos.read(bufferArxiu);
estatEdit.setText("Recibiendo archivo....");
archivo.write(bufferArxiu);
tamArxActual = archivo.length();
Thread.sleep(2);
}
sockDatos.setSoTimeout(0);
estatEdit.setText("Archivo recibido");
str2 = "";
clientSocket.setSoTimeout(30000);
while(true && (!str2.contains("PDA3-F#S-") && !str2.contains("PDAERR1-F#S-")))
{
s2 = entrada.read(buffer2);
str2 = new String(buffer2, "UTF8");
}
clientSocket.setSoTimeout(0);
if(str2.contains("PDA3-F#S-"))
{
sortida.writeBytes("FAS7-F#S-");
Thread.sleep(2000);
sortida.writeBytes("FAS6-F#S-");
}
sockDatos.close();
clientSocket.close();
String pathZip = Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/sincroorders.zip";
String directoriExtraccio = Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/";
String pathTxt = Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/SincrofitPC.txt";
UnzipUtility unzipper = new UnzipUtility();
try {
unzipper.unzip(pathZip, directoriExtraccio);
} catch (Exception ex) {
System.out.print("No s'ha pogut descomprimir l'arxiu");
ex.printStackTrace();
}
File f = new File(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/SincrofitPC.dll");
f.renameTo(new File(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/SincrofitPC.txt"));
importarSincro();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void importarSincro() throws SQLException, IOException
{
int contador = 0;
String LiniaSQL;
FileInputStream fstream = new FileInputStream(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/SincrofitPC.txt");
DataInputStream DataInFile = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(Environment.getExternalStorageDirectory()+"/OrdersCE/Syncro/SincrofitPC.txt"), "Latin-1"));
String FinalSQL = "";
baseDatos = openOrCreateDatabase("BBDD", MODE_WORLD_WRITEABLE, null);
baseDatos.execSQL("BEGIN");
while ((LiniaSQL = br.readLine()) != null) {
if(contador > 0)
{
FinalSQL = LiniaSQL.replace("***** ", "");
if(FinalSQL.contains("DELETE") && !FinalSQL.contains("DELETE FROM"))
{
FinalSQL = FinalSQL.replace("DELETE", "DELETE FROM");
try{
baseDatos.execSQL(FinalSQL);
}catch(SQLiteException e){
}
}
else
{
try{
baseDatos.execSQL(FinalSQL);
}catch(SQLiteException e){
}
}
LiniaSQL = "";
}
contador++;
}
baseDatos.execSQL("COMMIT");
estatEdit.setText("Tareas completadas!");
baseDatos.close();
}
/////////////////////////////////////////
public static String deserializeString(File file)
throws IOException {
int len;
char[] chr = new char[4096];
final StringBuffer buffer = new StringBuffer();
final FileReader reader = new FileReader(file);
try {
while ((len = reader.read(chr)) > 0) {
buffer.append(chr, 0, len);
}
} finally {
reader.close();
}
return buffer.toString();
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Thanks in advance!
David.
Internet connection is a blocking action, i.e. it blocks the UI until the action is complete (this is why only the last update is seen). You have to move it to a Thread (or even better to an AsyncTask). But then you get another problem, you cannot update UI from outside the main Thread. So, you have to use a Handler to update the UI.
You are executing a network operation on main thread, point to understand here is Android operates upon single main thread so if you do any time consuming operation on Service/Activity/Content Providers etc, it will block your main thread and hence all the stuff will look like they are blocked/un-responsive infact you will receive an ANR message after few seconds.
to conclude, follow this approach
Move your network extensive code to a separate thread
Implement a Mesasge handling mechanism to update your UI on main thread (Handler/Looper)
to help out things further Android provides you a class AsynTask for this purpose. You can do your operation in doInBackground() and upon condition you can update you UI through onProcessUpdate()
hope this helps.
If you run this process on UI thread it may cause to prompt a dialog saying "Applicaiton
not responding".So I recommend this task must be done either in AsyncTask or Service
U can update your text view in
onPreExecute()
onProgressUpdate()
onPostExecute() functions in AsyncTask.
Eg.for downloading file
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
When I try this code, it starts download but then stuck with alert "force close"
what should I do? Use some kind of background thread?
try {
long startTime = System.currentTimeMillis();
URL u = new URL("http://file.podfm.ru/3/33/332/3322/mp3/24785.mp3");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("/sdcard/","logo.mp3"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) != -1 ) {
f.write(buffer,0, len1);
}
f.close();
Log.d("ImageManager", "download ready in" +
((System.currentTimeMillis() - startTime) / 1000) + " sec");
}
catch (IOException e)
{
Log.d("ImageManager", "Error" +
((System.currentTimeMillis()) / 1000) + e + " sec");
}
I was dealing with similar problem last week and ended up using AsyncTask with progress bar displayed since it could take some time for the file to be downloaded. One way of doing it is to have below class nested in your Activity and just call it where you need to simply like this:
new DownloadManager().execute("here be URL", "here be filename");
Or if the class is not located within an activity and calling from an activity..
new DownloadManager(this).execute("URL", "filename");
This passes the activity so we have access to method getSystemService();
Here is the actual code doing all the dirty work. You will probably have to modify it for your needs.
private class DownloadManager extends AsyncTask<String, Integer, Drawable>
{
private Drawable d;
private HttpURLConnection conn;
private InputStream stream; //to read
private ByteArrayOutputStream out; //to write
private Context mCtx;
private double fileSize;
private double downloaded; // number of bytes downloaded
private int status = DOWNLOADING; //status of current process
private ProgressDialog progressDialog;
private static final int MAX_BUFFER_SIZE = 1024; //1kb
private static final int DOWNLOADING = 0;
private static final int COMPLETE = 1;
public DownloadManager(Context ctx)
{
d = null;
conn = null;
fileSize = 0;
downloaded = 0;
status = DOWNLOADING;
mCtx = ctx;
}
public boolean isOnline()
{
try
{
ConnectivityManager cm = (ConnectivityManager)mCtx.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
catch (Exception e)
{
return false;
}
}
#Override
protected Drawable doInBackground(String... url)
{
try
{
String filename = url[1];
if (isOnline())
{
conn = (HttpURLConnection) new URL(url[0]).openConnection();
fileSize = conn.getContentLength();
out = new ByteArrayOutputStream((int)fileSize);
conn.connect();
stream = conn.getInputStream();
// loop with step
while (status == DOWNLOADING)
{
byte buffer[];
if (fileSize - downloaded > MAX_BUFFER_SIZE)
{
buffer = new byte[MAX_BUFFER_SIZE];
}
else
{
buffer = new byte[(int) (fileSize - downloaded)];
}
int read = stream.read(buffer);
if (read == -1)
{
publishProgress(100);
break;
}
// writing to buffer
out.write(buffer, 0, read);
downloaded += read;
// update progress bar
publishProgress((int) ((downloaded / fileSize) * 100));
} // end of while
if (status == DOWNLOADING)
{
status = COMPLETE;
}
try
{
FileOutputStream fos = new FileOutputStream(filename);
fos.write(out.toByteArray());
fos.close();
}
catch ( IOException e )
{
e.printStackTrace();
return null;
}
d = Drawable.createFromStream((InputStream) new ByteArrayInputStream(out.toByteArray()), "filename");
return d;
} // end of if isOnline
else
{
return null;
}
}
catch (Exception e)
{
e.printStackTrace();
return null;
}// end of catch
} // end of class DownloadManager()
#Override
protected void onProgressUpdate(Integer... changed)
{
progressDialog.setProgress(changed[0]);
}
#Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(/*ShowContent.this*/); // your activity
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Downloading ...");
progressDialog.setCancelable(false);
progressDialog.show();
}
#Override
protected void onPostExecute(Drawable result)
{
progressDialog.dismiss();
// do something
}
}
You should try using an AsyncTask. You are getting the force quit dialog because you are trying to do too much work on the UI thread and Android judges that your application has become unresponsive.
The answers to this question have some good links.
Something like the following would be a good start:
private class DownloadLargeFileTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog;
public DownloadLargeFileTask(ProgressDialog dialog) {
this.dialog = dialog;
}
protected void onPreExecute() {
dialog.show();
}
protected void doInBackground(Void... unused) {
downloadLargeFile();
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
and then execute the task with:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading. Please Wait...");
new DownloadLargeFileTask(dialog).execute();