using dialogeFragment from AsyncTask - android

my application has a background AsyncTask which checks every few second if there's a new reminder if there is an new reminder it should start a dialogeFragment which shows the reminder message.
the problem is from what i read that a dialogeFregment can be used only from an activity
can you maybe share an idea about how to implement this?
this is our current code:
public class checkReminderTask extends AsyncTask<FragmentManager, String, Void> {
Context context;
private MainActivity activity;
FragmentManager fm;
checkReminderTask(Context app_context){
context=app_context;
objPlayer=MediaPlayer.create(context, R.raw.its_time_time_tim);
reminderTable = new ParseObject("ReminderTable");
settingsTable = new ParseObject("SettingsTable");
androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
ParseQuery query = new ParseQuery("SettingsTable");
query.whereEqualTo("userID", androidId);
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
//TODO something, throw exception
} else {
myNumber=object.getString("userNumber");
}
}
});
}
#Override
protected Void doInBackground(FragmentManager... arg) {
//infinitely loop, and move the ball accordingly
fm=arg[0];
while (1 < 2)
{
//downloading the reminder...
try
{
publishProgress(currentDateandTime,reminderExeTime,reciverNumber,message);
Thread.sleep(1000);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
if(values[0].equals(values[1])){
MessageReceivedReminder dialog = new MessageReceivedReminder();
dialog.setMessageArguments(context,values[2],values[3]);
dialog.show(fm, "MyDialog");
objPlayer.start();
}
else
{
Log.i("thread", "compareFailed");
}
}
code from the activity(trying to send the FragmentManager directly):
FragmentManager fm = getFragmentManager();
ringThread.execute(fm);
right now we are receiving a run time error because of a null pointer
does anyone have a better idea how we can do such a thing?
thanks!!

if you're passing the app context, this is why it's happening.
when inflating dialogs, you should always use the Activity context and never the getApplicationContext().
for more, read this: Android: ProgressDialog.show() crashes with getApplicationContext

Related

Launch ProgressDialog from

I have a class extends Application where I initialize some libraries needed by my application.
I want to launch a ProgressDialog from this class to notify user what App is doing.
Is possible launch these elements from Application classes??
Thanks
ps. attach my code
public class ApplicationLoader extends Application implements InitializeDelegate {
public static String TAG = "ApplicationLoader";
private ProgressDialog pd = null;
private InitializeDelegate initializeDelegate = null;
#Override
public Context getApplicationContext() {
return super.getApplicationContext();
}
#Override
public void onCreate() {
super.onCreate();
// LOAD ANDROID LIBRARY //
initializeDelegate = this;
AndroidLibraries androidLibraries = new AndroidLibraries();
androidLibraries(getApplicationContext(), initializeDelegate);
try {
pd = new ProgressDialog(getApplicationContext());
pd.setMessage("Initializing..");
pd.show();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void initializingResult(InitializinResult initializingResult) {
Log.i(TAG,""+ initializingResult);
try {
if (pd != null) {
if (pd.isShowing()) {
pd.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Why not?
But don't do any heavy stuff in the UI Thread. You may use a splash screen in conjunction with a progress bar.

Second time running the thread makes application crash

I use a worker thread to read text from a url. My thread is as follow. In the first time running, I am sure thread running is finished as I can check sdcard_readstr is null.
In the second time running, when I call thread_download.start();, then the program crashed.
What could be wrong? Thanks
public class DownloadingThread extends AbstractDataDownloading {
#Override
public void doRun() {
// TODO Auto-generated method stub
try {
// Create a URL for the desired page
URL url = new URL(SDcard_DetailView.textfileurl);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
do{
sdcard_readstr = in.readLine();
}while(sdcard_readstr!=null);
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}
public abstract class AbstractDataDownloading extends Thread{
private final Set<ThreadCompleteListener> listeners
= new CopyOnWriteArraySet<ThreadCompleteListener>();
public final void addListener(final ThreadCompleteListener listener) {
listeners.add(listener);
}
public final void removeListener(final ThreadCompleteListener listener) {
listeners.remove(listener);
}
private final void notifyListeners() {
for (ThreadCompleteListener listener : listeners) {
listener.notifyOfThreadComplete(this);
}
}
#Override
public final void run() {
try {
doRun();
} finally {
notifyListeners();
}
}
public abstract void doRun();
}
EDIT1:
In my thread complete notification, I use runOnUiThreadto use the UI components.
Is that causing problem?
public void notifyOfThreadComplete(Thread thread) {
// TODO Auto-generated method stub
if(downloadingStopbuttonispressed == false){//background process completed
textfileurl = null;
this.runOnUiThread(new Runnable() {
public void run() {
Wifibutton = (Button) findViewById(R.id.Wifiscanning);
Wifibutton.setText("Load another day's data");
final MenuItem refreshItem = optionsMenu.findItem(R.id.airport_menuRefresh);
refreshItem.setActionView(null);
}
});
}
}
I called thread start in onResume() as
#Override
protected void onResume() {
super.onResume();
if(textfileurl != null){
Wifibutton.setText("Stop Data Loading");
buttonStatus = "loading";
setRefreshActionButtonState(true);
thread_download.start();
}
}
EDIT2:
My LogCat image is attached.
My solution is here . I can't reuse the same instance of the Thread object in the second time. I need to create a new instance to call the Thread in the second time. So Thread is suitable for single time running process, for multiple time running process I should use AsyncTask. Even AsyncTack is only for one time execution and for multiple time execution, we should use as new MyAsyncTask().execute(""); I don't understand why people downvote with no reason given. I couldn't find the link in my first search.

Releasing instance of Fragment without UI when setRetainInstance(true)

I need to add a row to my SQLiteDatabase and as a result i want to obtain newly created ID of the row. I extended AsyncTask class in order to run this task in the background thread, here it is:
private class InsertLangTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
if (mDB.isOpen()) {
try {
mId = mDB.insertOrThrow(DBS.D.TN, null, mCV);
}
catch (SQLException e) {
StringBuilder query = new StringBuilder();
query.append("SELECT " + BaseColumns._ID + " FROM " + DBS.D.TN + " WHERE ");
Object[] bindArgs = new Object[mCV.size()];
int i = 0;
for (Map.Entry<String, Object> entry: mCV.valueSet()) {
query.append((i > 0) ? " AND " : "");
query.append(entry.getKey());
query.append(" = ?");
bindArgs[i++] = entry.getValue();
}
SQLiteStatement statement = mDB.compileStatement(query.toString());
for (i = 0; i < bindArgs.length; i++) {
DatabaseUtils.bindObjectToProgram(statement, i + 1, bindArgs[i]);
}
try {
mId = statement.simpleQueryForLong();
} finally {
statement.close();
}
}
}
}
catch (NullPointerException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
if (mActivity != null) { mActivity.onLangInserted(mId); }
super.onPostExecute(result);
}
}
This InsertLangTask class is enclosed in Fragment class where i implements only onCreate(), onAttach() and onDetach(), also i create a method inside to initialize members (cannot do it in constructor, as it must be empty in Fragment classes). I wrap AsyncTask in this Fragment class in order to preserve my mId member and pass it back to my activity even if the configuration was changed and activity was destroyed. I know that it's not exactly the case of long-term operation, but it can possibly be. Here are the parts of this class:
public class InsertLangFragment extends Fragment {
public static interface OnLangInsertedListener {
void onLangInserted(long id);
}
SQLiteDatabase mDB = null;
ContentValues mCV = null;
private OnLangInsertedListener mActivity = null;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
public void RunTask(SQLiteDatabase db, ContentValues cv) {
new InsertLangTask().execute();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onDetach() {
super.onDetach();
mActivity = null;
}
}
And in my Activity i just write this when i want to start this process:
InsertLangFragment fragment = new InsertLangFragment();
getFragmentManager().beginTransaction().add(fragment, "insertTaskFragment").commit();
fragment.RunTask(mSQLiteDatabase, mContentValues);
Obtaining mId in implemented callback
public void onLangInserted(long id) {
}
I used setRetainInstance(true) so this fragment will never be destroyed (except back button) and will be located in memory all time. The question is how should i properly destroy fragment instance? I suppose that i have three possible outcomes:
1) It's not necessary to destroy this Fragments.
2) I should put getFragmentManager().remove("insertTaskFragment").commit() at the end of onLangInserted() in my Activity
3) I should put setRetainInstance(false) at the end of onPostExecute() in AsyncTask.
Please give me an advice how should i manage this Fragment properly.
Thank you. Please don't be too strict, i'm a newbie here.

Performing data loading unitl succesfull or user break

In my app I performing loading data from web and then displaying it to user. Before loading data app shows progress dialog. I have problem if user locks phone in the middle of loading operation, or server is overloaded and can't respond in time my application freezes, because it doesn't dismiss progress dialog, or in some cases it crashes because lack on needed data.
If some error happened while loading data I want show some dialog to user to let him know about error and ask him should application repeat last request. I tried to use AlertDialog for it, but I haven't succeed.
Here is code of one activity (There is no progress dialog here, but it demonstrates how I loading data):
#EActivity(R.layout.layout_splash)
#RoboGuice
public class SplashScreenActivity extends Activity {
#Inject
private AvtopoiskParserImpl parser;
#Bean
BrandsAndRegionsHolder brandsAndRegionsHolder;
#ViewById(R.id.splash_progress)
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadData();
}
#Background
protected void loadData() {
publishProgress(10);
LinkedHashMap<String, Integer> brands = null;
try {
brands = parser.getBrands();
} catch (IOException e) {
Log.e(e.getMessage());
}
publishProgress(50);
LinkedHashMap<String, Integer> regions = null;
try {
regions = parser.getRegions();
} catch (IOException e) {
Log.e(e.getMessage());
}
publishProgress(70);
populateData(brands, regions);
}
#UiThread
protected void populateData(LinkedHashMap<String, Integer> brands, LinkedHashMap<String, Integer> regions) {
Intent intent = new Intent(SplashScreenActivity.this, SearchActivity_.class);
brandsAndRegionsHolder.brandsMap = brands;
brandsAndRegionsHolder.regionsMap = regions;
publishProgress(100);
startActivity(intent);
finish();
}
#UiThread
void publishProgress(int progress) {
progressBar.setProgress(progress);
}
}
parser.getBrands() and parser.getRegions() are loading data from the web.
I want to do something like this:
boolean repeatRequest = true;
while (repeatRequest) {
try {
brands = parser.getBrands();
repeatRequest = false;
} catch (IOException e) {
Log.e(e.getMessage());
repeatRequest = showErrorDialog();
}
}
But I didn't manage to do so because this code executes in background thread, but dialog should be shown in UI thread.
I believe that it should be standard approach of doing so, but didn't manage to find it.
Any ides how can I implement this?
The best way is to use AsyncTask.
private class LoadDataTask extends AsyncTask<Void, Integer, Object> {
private ProgressDialog mProgress;
protected Object doInBackground(Void... params) {
// This method runs in background
Object result = null;
try {
result = parser.parse();
} catch (Exception e) {
result = e.getMessage();
}
return result;
}
protected void onProgressUpdate(Integer... progress) {
// This method runs in UI thread
mProgress.setProgress(progress[0]);
}
protected void onPreExecute() {
// This method runs in UI thread
mProgress = new ProgressDialog(context);
mProgress.show();
}
protected void onPostExecute(Object result) {
// This method runs in UI thread
mProgress.dismiss();
if (result instance of String) {
// Here you can launch AlertDialog with error message and proposal to retry
showErrorDialog((String) result);
} else {
populateData(result);
}
}
}

Android: How do I make a ListView update itself periodically?

I never got this working in a straightforward manner. Sorry if I'm being a little vague. I'll try to elaborate on what I'm trying to do. I am trying to build a listview that grabs its data from a webservice. Once I initialize a listview, I want to keep polling the webserver periodically and update the contents of the listview. For this I am doing something like this:
public class SampleAutoUpdateList extends Activity {
//Autoupdate handler
private Handler handler = new Handler();
private Runnable updater = new Runnable() {
public void run() {
/*
* Update the list
*/
try {
Log.i("UPDATE", "Handler called");
searchAdapter = getFeed(URL);
searchAdapter.notifyDataSetChanged();
handler.postDelayed(this, Configuration.REFRESH_INTERVAL);
} catch(Exception e) {
Log.e("UPDATE ERROR", e.getMessage());
}
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linearmode);
this.context = this;
searchAdapter = getFeed(URL);
LinearLayout l2 = (LinearLayout) findViewById(R.id.secondaryLayout);
ListView list = new ListView(context);
l2.addView(list);
// display UI
UpdateDisplay(list);
updater.run();
}
private SearchAdapter getFeed(String URL) {
try
{
SearchHandler handler = new SearchHandler();
URL url = new URL(URL);
String data = convertStreamToString(url.openStream());
data = data.substring(data.indexOf('['), data.length()-1);
handler.parseJSON(data);
return handler.getFeed();
}
catch (Exception ee)
{
// if we have a problem, simply return null
Log.e("getFeed", ee.getMessage());
return null;
}
}
private void UpdateDisplay(View searchView) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
searchList = (ListView) searchView;
myProgressDialog = ProgressDialog.show(this,
"Please wait...", "Loading search....", true);
new Thread() {
public void run() {
try{
Thread.sleep(2000);
} catch (Exception e) { }
runOnUiThread(new Runnable() {
#Override
public void run() {
if (searchAdapter == null)
{
Log.e("ERROR", "No Feed Available");
return;
}
searchAdapter.setContext(context);
searchList.setAdapter(searchAdapter);
searchList.setSelection(0);
}
});
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
}
And the SearchHandler class is simple:
public class SearchHandler extends DefaultHandler {
SearchAdapter _adapter;
SearchItem _item;
public SearchHandler()
{
}
public SearchAdapter getFeed()
{
return _adapter;
}
public void parseJSON(String data) {
// TODO Auto-generated method stub
_adapter = new SearchAdapter();
JSONArray parseArray;
try {
parseArray = new JSONArray(data);
for (int i=0; i < parseArray.length(); i++) {
SearchItem item = new SearchItem();
JSONObject jsonUser = parseArray.getJSONObject(i);
item.set_from(jsonUser.getString ("from"));
item.set_msg(jsonUser.getString("msg"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No matter what I do, the handler gets called and the new items are fetched, but the list is never refreshed... Any ideas on what could be going wrong?
Well, it is a little bit difficult to follow your code, since you only have a fragment of it, and few of the really relevant bits. For example, based on your available code, your list should be forever empty, since you never associate the searchAdapter with a ListView...at least in the code you have shown.
That being said, the following lines seem particularly odd:
searchAdapter = getFeed(URL);
searchAdapter.notifyDataSetChanged();
I am going to assume that getFeed() (not shown) creates a new ListAdapter of some sort. If getFeed() is creating a new ListAdapter, there is no need to call notifyDataSetChanged() on it, as its data set hasn't changed -- it's brand new. Moreover, unless you are associating this new ListAdapter to your ListView, the new ListAdapter will have no effect.
If I'm barking up the wrong tree, consider adding lines to your sample showing the implementation of getFeed() and where you are using searchAdapter.

Categories

Resources