android-context

Android Get Application's 'Home' Data Directory

浪子不回头ぞ 提交于 2019-12-17 08:30:10
问题 A simple question, relating to the default 'home' directory when an app writes to the internal memory. By default, any files created are placed by the OS (2.2) in: /data/data/your.package/files When reading in files, the same default is used, when keeping in proper context via openFileInput() , openFileOutput() . But if I need to check file existence, for instance, using the File class, I need to specify the whole path in the constructor. I see there are Environment.getDataDirectory()

How to retrieve a context from a non-activity class?

偶尔善良 提交于 2019-12-17 07:47:32
问题 I have found one answer that appears to say I should create a separate class and make a static MyApplication object and make a get method. Then any class can call MyApplication.get() to retrieve the context. Is there any other cleaner way? This is my situation: I have a class A and a class B. Class A contains an object from class B (let's call the object b). In class A I call, "b.play()". However, I get a null pointer exception because class B needs to pass a context to the MediaPlayer.create

Context not recognisied:Method not applicable for arguments

独自空忆成欢 提交于 2019-12-14 03:34:03
问题 I'm trying to make a timer to play an MP3 file every 1.5 seconds(a beep) in my android application. I have the following code and receive the error "The method create (context,int) in the type MediaPlayer is not applicable for the arguments (Beep.RemindTask,int)" in my run function below: package com.example.timer; import java.util.Timer; import java.util.TimerTask; import android.media.AudioManager; import android.media.MediaPlayer; public class Beep { Timer timer; public Beep() { timer =

Full example or tutorial about how to get Context from data binding android [closed]

老子叫甜甜 提交于 2019-12-14 03:31:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm a beginner in android. I have a problem like this issue Android DataBinding where to get context? Need to get context for formatting date and time of textview with data binding in correctly ways. However I don't understand the answer clearly. Anyone helps me to get context from data binding in correctly ways

Use interface to provide Android context for singleton?

徘徊边缘 提交于 2019-12-14 03:06:54
问题 I've been looking through the forks of a popular library that keep an ApplicationContext as a member field within a singleton. This make the app memory intensive as the process is a bit expensive. Some forks claim to solve the problem by using an anonymous class implement an interface to provide the Context for the singleton in demand (here and here). In general, the code can be summarized like this: The interface: public interface FFbinaryContextProvider { Context provide(); } The singleton:

error: cannot find symbol method contextModule(ContextModule)

徘徊边缘 提交于 2019-12-13 20:30:18
问题 I am developing news app but I am getting the following error from Logcat error: cannot find symbol method contextModule(ContextModule) below screenshot of error screenshot of error below BBCFragment.java class public class BBCSportFragment extends Fragment implements ArticleAdapter.ClickListener { public List<Article> articleList = new ArrayList<>(); @ActivityContext public Context activityContext; @ApplicationContext public Context mContext; @BindView(R.id.recycler_view) RecyclerView

Android studio say static field will leak contexts

可紊 提交于 2019-12-13 16:13:09
问题 Here is a similar question but I think it is not identical And I read Setting up request queue tutorial here In this page they write the following code public class MySingleton { private static Context mCtx; And I wrote same code in my project public class VolleySingleton { private static Context mContext; Android studio say “Do not place Android context classes in static fields; this is a memory leak”. What does it mean? And why dos the official android developer website use such kind of

How to show popup message without having a context

旧时模样 提交于 2019-12-13 13:01:15
问题 We are developing a library that will be used both in JVM and Android . And now we need to implement trial period notification. In other platforms (.Net and Mac) showing a popup message box was enough. But so far I cannot find a way to do this for Android. The problem is that to show some message (toast or AlertDialog), I need to have an actual context. But since our library doesn't contain UI elements and not related to UI in any way we are not asking a user to pass context when creating out

Passing reference to activity to utility class android

六眼飞鱼酱① 提交于 2019-12-13 11:36:28
问题 I realise this question has been asked many times, but I am still unable to completely understand this concept. In my application, I am using a static utility class to keep common methods (like showing error dialogs) Here is how my static class looks like: public class GlobalMethods { //To show error messages public static final void showSimpleAlertDialog(final Activity activity, String message, final boolean shouldFinishActivity) { if (!activity.isFinishing()) { AlertDialog.Builder builder =

Can't access getContentResolver() from context?

你说的曾经没有我的故事 提交于 2019-12-13 08:58:04
问题 I have two classes, MainActivity and DoHardWork . DoHardWork extends AsyncTask , and in the class I need to access a context in order to get the contentResolver and do a query. No problem, right? Let's just pass it as a parameter to DoHardWork : Context currCont = this; new DoHardWork(currCont).execute(); Then in the constructor I grab the context and store it in a global variable called ccc . But as soon as I try to access the context it crashes, with no errors that make sense. try { Cursor