android-context

Do not place Android context classes in static fields; this is a memory leak

坚强是说给别人听的谎言 提交于 2019-12-19 09:28:27
问题 I have a service which has a BeaconNotificationsManager , I want to access this BeaconNotificationsManager in my Activity . Currently my BeaconNotificationsManager is static : public class MyService extends Service { public static BeaconNotificationsManager bnm; } And I am accessing this in my Activity like this: if(MyService.bnm != null){ // do stuff } Although Android is telling me this is bad. What is the correct way to do this? 回答1: About Static issue: let just say you are referencing

Android context outside of Activity class

梦想与她 提交于 2019-12-18 15:03:25
问题 I am trying to handle exceptions in my application. I am trying to log the exception, then use Toast to alert the user that there was a problem. I have this working find in all my class's that extend Activity. However, in any class that does not extended activity I can not use the toast method as I can't get the current context. Is there a simple way to get around this or should all my class's extend Activity? 回答1: You just pass Context When You call Non-Activity class from Activity class

How do I view Android application specific cache?

浪尽此生 提交于 2019-12-18 10:35:55
问题 Is there any way to dynamically view the application specific cache in Android? I'm saving images to the cache (/data/data/my_app_package/cache) and I'm 99% sure they're saving there, but not sure how long they're staying around. When I look in the cache using the DDMS File Explorer within Eclipse, it's always empty. I've also tried examining the appropriate cache dir in ADB and again it's always empty. Any suggestions? 回答1: Unless ADB is running as root (as it would on an emulator) you

Extending Application

十年热恋 提交于 2019-12-18 09:49:13
问题 I'd like to extend Application in my Android app. I've done this such that I've created an extended Application object called MyApplication and added it to the manifest. I'd now like to add some getters and setters to hold some information. It looks like I'll need to pass the application Context to any classes which do not contain a Context. For example, say I create a class called MyObject (in its own java file): public class MyObject { public void doStuff() { // do stuff } } How might I

How to get Context through hooking in android

橙三吉。 提交于 2019-12-18 08:59:24
问题 The background is : I'm using xposed framework to hook a third party app. When I hook method XXX, xposed gave me "ClassNotFound" error. I checked and found the method XXX is in a dex file and would be loaded by DexClassLoader on the run. To hook the method XXX, I need to change the default ClassLoader in xposed to DexClassLoader . To get a DexClassLoader instance, I need a Context instance of the third party app. Here comes the question: how to get the context instance? I searched

Passing Activity or Context to other instance

纵饮孤独 提交于 2019-12-18 05:57:21
问题 Can you please tell me whats the best practice to pass activity or context to other instance(;) But..., What is better to pass? Activity or Context Is good idea to have the Activity as Global (public static Activity activity) here is my code. What would you change? (based on good android practices) 回答1: Passing an Activity into another object that does not specifically require the Activity object is usually a bad idea. Activity extends Context so it works to satisfy a method that requires

Add Context Menu Icon in android

两盒软妹~` 提交于 2019-12-17 23:27:41
问题 I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work public void onCreateContextMenu(ContextMenu menu , View v, ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite) .setIcon(android.R.drawable.btn_star); } 回答1: Context menus do not support icons. Note: Context menu items do not support icons or shortcut keys. 回答2: This library allows you to have a context menu

Is it possible to get application's context in an Android Library Project?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 15:35:31
问题 I would like to get the context of application which has reference/hosted my library at run-time inside one class of my library project. Is it possible? If yes, how? Thanks Update I don't want my user to pass context in parameter to my library project because it is possible that my library project will be called through JNI and I have no idea how I can get context in JNI and pass it to Java layer. 回答1: Is it possible? Yes. If yes, how? Pass it in as a parameter. I don't want my user to pass

Getting the Application Context

℡╲_俬逩灬. 提交于 2019-12-17 10:38:23
问题 This might be a simple question but I just wanted to make sure I am right. In my android application I have a constructor that uses: activity.getApplicationContext() The activity is passed into the constructor as a parameter. The problem is that I am calling this class from a Service. If I make a second constructor which accepts the Service as a parameter and uses service.getApplicationContext ? Will I get the same application context? 回答1: Will I get the same application context? Yes. You

How to get my activity context?

拜拜、爱过 提交于 2019-12-17 10:17:32
问题 I don't really get the idea behind how this whole thing works really, so if I have some class A that need the context of a class B which extends Activity , how do i get that context? I'm searching for a more efficient way than giving the context as a parameter to class A constructor. For example if class A is going to have millions of instances then we would end up having millions of redundant pointer to Context while we should be able somehow to have just one somewhere and a getter function.