AsyncTaskLoader is not working properly on fragment - android

I am trying to implement an AsyncTaskLoader running in a fragment and i don't know exactly the reason why onLoadFinished never is called. I am not sure if the context that i pass is the proper one.
This is the basic and custom AsyncTaskLoader:
public static class CustomAsyncLoader extends AsyncTaskLoader<String>
{
public CustomAsyncLoader(Context context) {
super(context);
// do some initializations here
}
#Override
protected void onForceLoad() {
// TODO Auto-generated method stub
super.onForceLoad();
}
#Override
public void deliverResult(String apps) {
}
#Override
protected void onStopLoading() {
// Attempts to cancel the current load task if possible
cancelLoad();
}
#Override
public void onCanceled(String apps) {
super.onCanceled(apps);
}
#Override
public String loadInBackground() {
String result = "";
// ...
// do long running tasks here
// ...
return result;
}
}
Here i will show you the 3 methods overwritted:
#Override
public Loader<String> onCreateLoader(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
return new CustomAsyncLoader(root.getContext());
}
#Override
public void onLoadFinished(Loader<String> arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "onLoadFinish", Toast.LENGTH_LONG).show();
}
#Override
public void onLoaderReset(Loader<String> arg0) {
// TODO Auto-generated method stub
}
In the method onResume of my fragment i am calling to init the loader:
getLoaderManager().initLoader(0, null, this).forceLoad();
and the last detail to comment is how the fragment implemented the loader callback:
public class FragmentName extends CustomFragment implements LoaderManager.LoaderCallbacks<String>
Let see if anybody could help me about how implement it. Thanks in advance.

You must call super.deliverResult(apps) in deliverResult method. Otherwise super class of your CustomAsyncLoader won't take care of delivering result to registered listener.

Related

How to get the context from a function of a class that extends Activity in android?

I have a class that extends Activity, When I try to access the context from the onCreate() method it gets printed but when I save it in a variable context and try to access it from the committext() function as shown below, It prints null. I also tried using "this" and getBaseContext() directly from the committext() function as it is a part of the same class, I get null pointer exception. Please help me figure out what is going wrong.
public class MainKeyboardActionListener extends Activity implements KeyboardView.OnKeyboardActionListener, View.OnTouchListener {
private Context context;
public static boolean active = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mode_keyboard, false);
context=getBaseContext();
System.out.println("contexxtt1"+context);
}
#Override
public void onStart() {
super.onStart();
active = true;
}
#Override
protected void onResume() {
super.onResume();
}
private static class myHandler extends Handler {
}
;
public void setInputConnection(InputConnection ic) {
}
#Override
public void onKey(int arg0, int[] arg1) {
// TODO Auto-generated method stub
}
#Override
public void onPress(int keyCode) {
}
#Override
public void onRelease(int keyCode) {
commitText(String.valueOf(keyCode));
}
private void handleException(int keyCode) {
}
private void removeHalantMode() {
}
private void commitText(String text) {
if(active==true) {
System.out.println("contexxtt"+context);
}
}
#Override
public void onText(CharSequence arg0) {
// TODO Auto-generated method stub
}
#Override
public void swipeDown() {
// TODO Auto-generated method stub
}
#Override
public void swipeLeft() {
// TODO Auto-generated method stub
}
#Override
public void swipeRight() {
// TODO Auto-generated method stub
}
#Override
public void swipeUp() {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
super.onPause();
}
#Override
public void onStop() {
super.onStop();
active = false;
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
Make your myHandler class not static.

Web View running on other than UI thread

I am trying to show flurry interstitial but getting following message in debug screen and I am not receiving interstitial on my screen.
07-14 15:55:31.390: W/webview(10588): java.lang.Throwable: Warning: A
WebView method was called on thread 'FlurryAgent'. All WebView methods
must be called on the UI thread. Future versions of WebView may not
support use on other threads.
I have followed this tutorial completely :
Android Integration
At present I am working on AndEngine. As error replying I put my all code in UI thread but result is same.
Here is my code for displaying ads :
protected void onCreate(Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
// configure Flurry
FlurryAgent.setLogEnabled(false);
// init Flurry
FlurryAgent.init(MainGameActivity.this, MY_FLURRY_APIKEY);
mFlurryAdInterstitial = new FlurryAdInterstitial(MainGameActivity.this,
MY_ADSPACE_NAME);
FlurryAdTargeting adTargeting = new FlurryAdTargeting();
// enable test mode for this interstitial ad unit
adTargeting.setEnableTestAds(true);
mFlurryAdInterstitial.setTargeting(adTargeting);
// allow us to get callbacks for ad events
mFlurryAdInterstitial.setListener(interstitialAdListener);
mFlurryAdInterstitial.fetchAd();
}
FlurryAdInterstitialListener interstitialAdListener = new FlurryAdInterstitialListener() {
#Override
public void onFetched(final FlurryAdInterstitial adInterstitial) {
adInterstitial.displayAd();
}
#Override
public void onError(final FlurryAdInterstitial adInterstitial,
FlurryAdErrorType adErrorType, int errorCode) {
adInterstitial.destroy();
}
#Override
public void onAppExit(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClicked(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClose(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDisplay(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onRendered(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onVideoCompleted(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(MainGameActivity.this);
}
#Override
protected void onStop() {
super.onStop();
FlurryAgent.onEndSession(MainGameActivity.this);
}
#Override
public void onDestroy() {
super.onDestroy();
mFlurryAdInterstitial.destroy();
}
So what can I do in this situation?

using gridview inside a runnable gives error

Can somebody explain how to use the GridView inside a Runnable.
Handler starthandler = new Handler();
Runnable startrun= new Runnable(){
#Override
public void run() {
G1=(GridView)findViewById(R.id.balloongrid);
G1.setAdapter(new BalloonImageAdaper(this));
}
};
The above code gives the following error:
The constructor Cacrballoonclass.BalloonImageAdaper(new Runnable(){}) is undefined
public class BalloonImageAdaper extends BaseAdapter{ // create a class by name
imageadapter so as to fill the gridview with data
private Context ballooncontext;
public BalloonImageAdaper (Context c) {
ballooncontext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return balloonthumbs.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView balloonimageview;
if(arg1==null){
balloonimageview = new ImageView(ballooncontext);
balloonimageview.setLayoutParams(new GridView.LayoutParams(75,75));
balloonimageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
balloonimageview.setPadding(7, 7, 7, 7);
}else {
balloonimageview = (ImageView)arg1;
}
// lets inflate balloon randomly
int rballoon = new Random().nextInt(ballooninstruct.length);
balloonimageview.setImageResource(balloonthumbs[rballoon]);
balloonimageview.setTag(balloonthumbs[rballoon]);
return balloonimageview;
}
}// class imageAdapter extends baseadapter
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onResume();
finish();
}
}//class ends cacrballoonclasss
Replace
new BalloonImageAdaper(this)
with
new BalloonImageAdaper(mContext) // mContext: reference to any Context (Activity or whatever)
(If that's inside an Activity you can use NameOfActivity.this.)
The this inside your Runnable is referring to your Runnable and not, like I assume you want it to reference to, your Activity.

how to send data in service based android app

I want to send data from one application A to other application B using AIDL file. My appl A is like below,
public class LibValue {
public static native int intFromJNI(int n);
static {
System.loadLibrary("hello");
System.out.println("LibValue : Loading library");
}
I am getting value from JNI file to above class. The data from above class to send another app B using AIDL service is like below.
IEventService.aidl
interface IEventService {
int intFromJNI(in int n);
}
for this I written IEventImpl.java class
public class IEventImpl extends IEventService.Stub{
int result;
#Override
public int intFromJNI(int n) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("IEventImpl"+LibValue.intFromJNI(n));
return LibValue.intFromJNI(n);
}
}
To access above class I writtn service class like below
public class EventService extends Service {
public IEventImpl iservice;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("EventService", "indside onBind");
return this.iservice;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.iservice = new IEventImpl();
Log.i("EventService", "indside OncREATE");
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
this.iservice = null;
super.onDestroy();
}
All above classes are server side. The below class is Client(app) class to access data.
public class EventClient extends Activity implements OnClickListener, ServiceConnection{
public IEventService myService;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_client);
button = (Button)findViewById(R.id.button1);
this.button.setOnClickListener(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (!super.bindService(new Intent(IEventService.class.getName()),
this, BIND_AUTO_CREATE)) {
Log.w("EventClient", "Failed to bind to service");
System.out.println("inside on resume");
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
super.unbindService(this);
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
this.myService = IEventService.Stub.asInterface(service);
Log.i("EventClient", "ServiceConnected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.d("EventClient", "onServiceDisconnected()'ed to " + name);
// our IFibonacciService service is no longer connected
this.myService = null;
}
I am trying to access data from service class but not able to find out the way. can anybody tell how to access data from service to client application ?
Thanks

Android Facebook updating UI

I have this application which connects to Facebook. For some reason I'm getting this error:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
This is my code:
The activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (AppVariables)getApplicationContext();
context = getBaseContext();
facebook = app.getFacebook();
facebookRunner = new AsyncFacebookRunner(facebook);
facebookConnection = new FacebookConnection(this);
internetStatus = (TextView)findViewById(R.id.internetStatus);
facebookRunner.request("me", facebookConnection);
}
#Override
public void onFacebookResponse(String response) {
internetStatus.setText(response);
}
This is FacebookConnection Class:
public class FacebookConnection implements RequestListener {
private FacebookConnectionListener listener;
public FacebookConnection (FacebookConnectionListener listener)
{
this.listener = listener;
}
public void updateFacebookListener(String response)
{
listener.onFacebookResponse(response);
}
#Override
public void onComplete(String response, Object state) {
updateFacebookListener(response);
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
And this is my listener interface:
public interface FacebookConnectionListener {
public void onFacebookResponse(String response);
}
What's wrong?
You are using the AsyncFacebookRunner which make all of the API requests in an asynchronous fashion. To do that, it uses a different thread than the main one in which the activity is running.
When the async runner finishes it, it executes your onFacebookResponse method itself, instead of having the main thread executing it.
If your activity is just waiting for the graph request then don't use the AsyncFacebookRunner but the regular Facebook.
If not, then your activity should check if the data was returned, and if so update it in the original thread.
I've never had to do that before, so this is not from my personal experience, and I have not tested it, but from the code in that tutorial it seems like you just need to change your onFacebookResponse to something like this:
public void onFacebookResponse(String response) {
this.runOnUiThread(new Runnable() {
public void run() {
this.internetStatus.setText(response);
}
});
}

Categories

Resources