android-context

What is the Context passed into onReceive() of a BroadcastReceiver?

假装没事ソ 提交于 2019-12-06 01:59:43
What is the context that is passed int the onReceive method of a BroadcastReciver : public void onReceive (Context context, Intent intent) According to the official documentation : The Context in which the receiver is running. A little research gives below result... For static receiver public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("PANKAJ", "Context class " + context.getClass().getName()); Log.e("PANKAJ", "Application Context class " + context.getApplicationContext().getClass().getName()); } } I got below log 08-05

how to call non static method from static method in android

六眼飞鱼酱① 提交于 2019-12-06 01:34:34
I am facing a big problem in calling non static method from static method. This is my code Class SMS { public static void First_function() { SMS sms = new SMS(); sms.Second_function(); } public void Second_function() { Toast.makeText(getApplicationContext(),"Hello",1).show(); // This i anable to display and cause crash CallingCustomBaseAdapters(); //this was the adapter class and i anable to call this also } I am able to call Second_function but unable to get Toast and CallCustomBaseAdapter() method, crash occurs. What should I do to fix that issue ? public static void First_function(Context

Displaying a Toast message from the Application class

六月ゝ 毕业季﹏ 提交于 2019-12-06 01:25:01
问题 I have several classes in my application. Some are Activities, Services and Pure java classes. I know i can display a Toast message from within an Activity but i'd like to display a Toast from a pure java class. In the java class i pass a context in to the constructor but this doesn't seem to show the toast. I have created a method in the Application class that takes a String as an argument, hoping i could generate a Toast using the Application context, no joy here either. How can i generate

Calling method of another class using contexts

♀尐吖头ヾ 提交于 2019-12-06 00:00:10
问题 I have a custom titlebar with an ImageButton, which produces a dialogbox, and I want to be able to show location(place itemizedOverlay) on a map(in another class) when list item is selected from dialog box, and titlebar and map are in the same context. I read somewhere that I could call a method of another class using contexts. How can I do so? public class MyTitleBar extends RelativeLayout{ private Context context; public MyTitleBar(Context context, AttributeSet attrs) { super(context, attrs

Is it beneficial to keep a member reference to system services in Android?

拟墨画扇 提交于 2019-12-05 23:02:49
Suppose I have an android Activity / Service that is using system services such as PowerManager, WifiManager, etc... Is it beneficial to have private members for keeping reference to those services once in the constructor or onCreate method instead of getting those service managers each and every time they are needed by calling getSystemService(...)`? If so, are those members safe to use in case one of those system services crash and restart? If they aren't safe, what is the right approach for handling such cases of service death? s it beneficial to have private members for keeping reference

Is each activity in android having its own context? What is the scenario with multiple activities in single application

坚强是说给别人听的谎言 提交于 2019-12-05 21:46:19
I was reading this link to understand context in the android. I still have one question that does each activity have its own context?. Please consider i am an android learner in beginning level. I still have one question that does each activity have its own context? Each Activity is its own Context . Activity is a Java class ; it inherits from Context : 来源: https://stackoverflow.com/questions/34269458/is-each-activity-in-android-having-its-own-context-what-is-the-scenario-with-mu

Android Activity Refreshing On Rotation

孤街醉人 提交于 2019-12-05 20:22:01
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 the CountDownTimer object and instead created a class internal to the activity which extended AsyncTask so it ran on a separate thread... No change. I figured being an internal class it's being reinitialised with the activity. Here's what I tried next; I made the class an external one. The Activity passes its Context to the class

SQlite Database: Unable to find context

妖精的绣舞 提交于 2019-12-05 14:38:46
Apparently there is some problem with the context and in the following line as I'm getting a NullPointerException . I think something is wrong with the context. Also when I"m trying to open and close database from two activities it's throwing CannotOpenORCloseDatabase Error. Please help. DBHelper dbHelper = new DBHelper(this.getApplicationContext()); Any idea why? Or it'd be great if someone could suggest a workaround. public static final String EXT_CATEGORY = "category"; public static final String EXT_URL = "url"; private String tableName = DBHelper.tableName; private SQLiteDatabase MyDb;

How to get Context when you call a method in a different class?

谁说我不能喝 提交于 2019-12-05 13:38:14
So basically, I have a method that requires a Context in order to perform several tasks. public void openDatePicker() { Context c; } I want to know, how to define a context of the method caller so I don't have to insert it as a parameter each time I call the method. Lets have an basic understanding of Context . Context allows access to application-specific resources and classes , as well as calls for application-level operations such as launching activities , broadcasting and receiving intents , etc. A Context represents your environment. It represents the state surrounding where you are in

Does the Android Application Context ever get destroyed?

偶尔善良 提交于 2019-12-05 02:00:00
I use my Android app's application Context as a storage area for "current state" information for my app. I'm finding that in the field, there are cases when this information goes away on some people's devices causing various NullPointerExceptions since I expect the data to be there when the app resumes and starts rebuilding the necessary activities. This usually happens when the user hits "Home", does something else, then eventually wanders back into the app - it attempts to go back to where it was before, but the application Context has mysteriously lost all its previously-saved state