FAQ Database Discussion Community


Android simulating play/pause button

android,android-intent,android-broadcast,keyevent,android-context
I'm trying to make my application open up Google Play Music and then simulate a play/pause command. My application is able to start Google Play Music successfully, but the downIntent and upIntent KeyEvents are never broadcast. I suspect this is because calling the method just invokes the method on my...

getBaseContext() and getApplicationContext()

android,android-context
In the following code, there is an error in getBaseContext() and getApplicationContext(), both of them are red in color. Does anyone know how to solve them? Moreover, I also want to know when to use these two methods. I have searched in internet, but I didn't understand. Can anyone simply...

Where can I get a better understanding of the overall and indepth understanding of instantiation with Context parameters?

java,android,sqlite,this,android-context
I am trying to resolve the connection between Context and Classes in this code and my understanding of this concept. The MainActivity.this does not work here. It came with the surrounding code. package com.Table; import android.content.Context; import android.graphics.Color; import android.os.AsyncTask; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.RelativeLayout;...

Context Error when making call from Broadcast Receiver

android,broadcastreceiver,android-context
My app checks the date in onResume to see if a method named changeReminder, in the MainActivity, needs to be called. That is, if the date is different from the stored date, changeReminder is called (changeReminder is called from other methods also, such as a refresh button). changeReminder in turn...

Is it really necessary to propagate context through superclasses to create a simple bitmap from drawables?

android,android-drawable,android-context
I have a simple problem, which has been answered many times on SO before, but I don't understand the answers and I can't get my code to work. I want to create a bitmap from a drawable. public class Helicopter extends Sprite { private Context context; private Bitmap bitmap =...

Toast in AsyncTask in non Activity class

android,android-asynctask,toast,android-context
I have a non Activity class which contains an AsyncTask which calls an URL. If the Connection Timeouts for some reasons, I want to let the user know this by posting a Toast. But I just can't get any Context. How can achive this? RPIcall.class import android.os.AsyncTask; import android.util.Log; import...

Android Activity Refreshing On Rotation

android,android-activity,android-asynctask,android-context
I'm an experienced .NET dev, but this is my first Android app and it's driving me nuts! It's a countdown timer which works fine, but when you rotate the screen it reinitialised the activity which zeroed and stopped the count down. This is what I tried; I got rid of...

android - update specific activity UI from onReceive

android,android-asynctask,broadcastreceiver,android-context
I have a BroadcastReceiver, and the onReceive is called from two different postExecute methods in two different asyncTasks, in two different Activities. I have a third activity that is running all the time called HomeActivity, and I want to publish some text to the HomeActivity's UI from the onReceive method....

Android: OnClickListener of Button on CardView

onclicklistener,android-context,android-cardview,android-recyclerview
I'm a bit of a newbie so I apologize if this is an easy question. I'm following this tutorial: http://www.truiton.com/2015/03/android-cardview-example/#comment-7174 It shows how to make a few CardViews in a layout with 2 TextViews in each - all programmatically. I would like to modify it to have a Button instead...

android service, context, builder

java,android,android-service,builder,android-context
I read around in Android Developer, but I didn't understand completely those 3 "ideas": 1) context 2) builder 3) service and how do they relate to each other? I saw for example: GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context) and I didn't get exactly what the general purpose of each "idea"....

Why SharedPreferences use the context?

android,android-intent,sharedpreferences,android-context
I try to use SharedPreferences, but I don't understand why we need the context. If I create a SharedPreferences with the context of my main activity, can I get something with another context? For example with an intentService class Thanks...

Using context reference after activity be finished

android,android-activity,android-context
I have a complete button in page, when user click button, it will start an async task and pass the activity context into it, then finish the activity to dismiss the activity view. What will happen if the activity be finished but the background task still need to use the...

android - changing Activity UI from application class

android,user-interface,android-activity,android-context
I extended the Application class in order to create singleton-like object in android. in this object I have all the HTTP work with my server, and all the other activities can access it and call methods to GET, POST etc. Code: public class HttpManagerInstance extends Application { private HttpClient httpClient;...

NullPointerException on Context during Initializing LayoutInflater

android,layout-inflater,android-context
I'm fetching data thru an API and trying to place them in a ListView(I've wrapped them inside a bundle first), I'm using BaseAdapter. Problem is the Context/LayoutInflater that I've passed inside a fragment to the Adapter returns a NullPointer Exception //onPostExecute in AsyncTask @Override protected void onPostExecute(Bundle bundle) { if(bundle...

Using getApplicationContext() in another simple class, NOT in MainActivity.java

java,android,android-context
I've created a simple class for my Android application called coreTuts. I'm done with tying it in to my MainActivity.java, activity_main.xml, and so on, like this: MainActivity.java coreTuts tuts = new coreTuts(); public void displayToast(View view) { tuts.sampleToast(); } This is what my coreTuts.java looks like: coreTuts.java public class coreTuts{...

Showing an AlertDialog once the activity has closed

java,android,callback,android-alertdialog,android-context
I am in the habit of passing this to any methods that take a Context. If I am inside an inner class, I pass OuterClassName.this. This may be bad practice, but I've never found a clear answer to what I should be doing. I have an activity that loads some...

I have error when use context in adapter, i would like use startActivityForResult(…) in adapter, how should I do?

android,android-intent,android-activity,android-adapter,android-context
and thank you in advance for your suggestions. MainActivity.java RecyclerView.Adapter mAdapter; @Override ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); mAdapter = new MyAdapter(getBaseContext(),TITLES,ICONS,NAME,EMAIL,PROFILE); mRecyclerView.setAdapter(mAdapter); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); ...} @Override protected...

Using broadcastreciever only on certain activity

android,android-intent,broadcastreceiver,android-context
I have two activities on is Register() and the other is ReadNews(). I am using broadcast receiver to detect internet connection automatically to execute some code. public void onReceive(Context context, Intent intent) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetwork != null && wifiNetwork.isConnected()) { if(MyApplication.isActivityVisible()...

OpenGL ES context squashed on unlock - landscape to portrait

android,opengl-es,screen-orientation,android-context,unlock
Working on a game that only runs in Portrait mode, I tried forcing sensorPortrait via the manifest: android:configChanges="locale|orientation|keyboardHidden|screenSize|screenLayout" android:screenOrientation="sensorPortrait" But when I lock the device in portrait, rotate it to landscape and then unlock it this happens: And this is How it looks in portrait: I think this happens because...

What is the difference between this, getContext() and getActivity()?

android,android-activity,android-context
I am very confused with the usage of all these that where should we use them.

Android JUnit4 Testing - Where to get Context from?

android,sqlite,unit-testing,android-context
I have to build an app with sqlite usage. Now I want to write my unit tests. This unit Tests should test my class SQLiteBridge. SQLiteBridge provides DAOs for every child class of Model. Now I got the problem that I need a context to create my SQLiteBridge. SQLiteBridge creates...

Why am I getting a NullPointerException when trying to use the reference of Activity from a Fragment?

android,android-fragments,nullpointerexception,android-context
I have a method that does a Facebook call for a user's profile info. And one of parameters is a Context. I am passing in the Activity's context from a Fragment, but some of my users are getting a NullPointerException for the context. I am saving the reference of the...

Safest approach for holding an instance of Activity

android,android-fragments,android-activity,android-context
What is the best and safer approach for holding an instance of the Activity private Activity mActivity; First approach: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = getActivity(); } Second approach: @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.from(getActivity()).inflate(R.layout.fragment_main, container, false);...

Use ApplicationContext if possible in Android?

android,applicationcontext,android-context
Is it a good practice to use application context instead of other context(Activity, Service and so on) if possible? Pros: Application context can help us avoid memory leak. Application context can help eliminate params in some circumstances Other things don't come to my head for now Cons: No clue for...

What am I doing wrong with my handling of Contex ( Java - Android)

android,android-context
I made a class (first block of code) that take in a bunch of data, creates 3 TextViews and then returns a RelativeLayout with all three them as children. Do accomplish this I needed to make the constructor of the class take in a argument for 'Context' since I need...

Call to dialog from View class with static method

android,methods,view,static,android-context
I have a problem. I would like call to Dialog from a View class. I use a public method. This is static. But when I use ... = new Dialog(getInstance()); or similar and then the app come there, it stops and the Log cat say me that argument context is...

unable to add window token null is not for an application when using accessibility in android

android,android-activity,android-context,android-dialog
I am trying to create a transparent activity in front of another app's login page. MyAccessibilityService Class extend the AccessibilityService class. When login page is identified the tranparentActivity() function will be called to show the dialog box, but I will get the error like unable to add window token null...

How to get context from ActivityManager.RunningTaskInfo?

android,android-context
I want to get context from the below code. I have not able to find the how to get activity so please help me in same. CODE : ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo ar = RunningTask.get(0); String activityOnTop=ar.topActivity.getClassName(); Thanks in Advance...

context cannot be resolved to a variable when using Picasso

android,picasso,android-context
Am trying to implement a Picasso, after importing the Picasso library, I get the following error on (context) saying context cannot be resolve to a variable. Please kindly find my entire code below, but this is the line that gives the error Picasso.with(context).load(myBitmap).into(imageView); public class WidgetActivity extends Activity { GridView...

Cannot resolve symbol 'context' in 'USING A PRELOADED SQLITE DATABASE WITH SQLITEOPENHELPER' example

android,sqlite,android-context
I'm using this class java to read db from assets folder and to copy to database folder. In page link, it say: To Use it ... Just use it normally as you would use an ordinary SQLiteOpenHelper object. Repository repo = Repository.getInstance( context ); SQLiteDatabase db = repo.getWritableDatabase(); ... but...

Context, storing internal memory and reading from it

android,storage,internal,android-context
Hello I have a problem with the context in the line productLookup(productId, Contexto), I can't get it right. What I'm trying to do is when I get the data from the DB via web, with the objects I also want to pass the ID of the objects to the productLookup...

Passing Activity Context to Handler(fragment) for Toast

android-fragments,handler,android-context,android-toast
I'm trying to use a Toast message in a handler fragment class but I can't access the main class' context. The handler is in the same class as the main activity, PlayFrets(which is also the UI thread).I've read through the plethora of questions on this topic on stackoverflow, but almost...

Do BroadcastReceiver, Service, and Activity share Application object?

android,android-activity,android-service,android-broadcast,android-context
I have among others the following components: a BroadcastReceiver registered in Manifest a STICKY Service normally started by boot complete which processes Intents from the above BroadcastReceiver an Activity which binds to the Service (starting it if for some reason does not exist) Under which circumstances do these components share...