android-context

android - How to get view from context?

岁酱吖の 提交于 2019-12-28 02:42:26
问题 I want to get the view or findViewById() from Context? Or from intent? I'm trying to reach a specific view in my broadcast receiver and the parameter of onReceive are context and intent. Well, I have a class and within it is my broadcast receiver. Now, I'm trying to separate the broadcast receiver from it, but I need a way so I can still communicate with the views on my class from my separated broadcast receiver class. Thanks. 回答1: For example you can find any textView: TextView textView =

Check if a device has flashlight [closed]

前提是你 提交于 2019-12-26 14:01:45
问题 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 6 years ago . public class FlashLightActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); Context context = this; PackageManager packageManager = context.getPackageManager(); // if device support camera? if (packageManager

Check if a device has flashlight [closed]

一世执手 提交于 2019-12-26 14:00:19
问题 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 6 years ago . public class FlashLightActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); Context context = this; PackageManager packageManager = context.getPackageManager(); // if device support camera? if (packageManager

Could not find method in a parent or ancestor Context for android:onClick attribute

≡放荡痞女 提交于 2019-12-25 17:17:54
问题 I have an image that accepts click and should open an xml layout when clicked. Here is the xml file that contains the clickable image: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context="com.abcd.myapp.myabcdapp.activities.BeginnersActivity"> <android

Using Activity class static members in a background service

北城以北 提交于 2019-12-25 14:50:32
问题 My app has an Activity class called MainActivity and one of its members is public static SharedPreferences prefsdefault; My app has also a service (in another process) which runs in background. Inside the service I wrote MainActivity.prefsdefault.getString(Key,"Hello"); The app sometimes throws null pointer exception at this line. Why? Does it mean that this member is cleaned up by the garbage collector when I close the activity and I cannot access it at anytime (when my service runs)? So

Using Activity class static members in a background service

╄→尐↘猪︶ㄣ 提交于 2019-12-25 14:49:27
问题 My app has an Activity class called MainActivity and one of its members is public static SharedPreferences prefsdefault; My app has also a service (in another process) which runs in background. Inside the service I wrote MainActivity.prefsdefault.getString(Key,"Hello"); The app sometimes throws null pointer exception at this line. Why? Does it mean that this member is cleaned up by the garbage collector when I close the activity and I cannot access it at anytime (when my service runs)? So

Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?

一世执手 提交于 2019-12-25 09:26:19
问题 I've got a singleton Toast-showing class, which keeps track of the current toast and cancels it when it is started again: public enum SingleToast { INSTANCE; private Toast currentToast; private String currentMessage; public void show(String message, int duration) { if (message.equals(currentMessage)) { currentToast.cancel(); } currentToast = Toast.makeText(App.getContext(), message, duration); currentToast.show(); currentMessage = message; } } (Extended explanation of this example can be

Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?

回眸只為那壹抹淺笑 提交于 2019-12-25 09:21:12
问题 I've got a singleton Toast-showing class, which keeps track of the current toast and cancels it when it is started again: public enum SingleToast { INSTANCE; private Toast currentToast; private String currentMessage; public void show(String message, int duration) { if (message.equals(currentMessage)) { currentToast.cancel(); } currentToast = Toast.makeText(App.getContext(), message, duration); currentToast.show(); currentMessage = message; } } (Extended explanation of this example can be

Getting resources from non-activity classes

北城以北 提交于 2019-12-25 04:21:41
问题 I have a special class that holds static info and makes calculations based on special circumstances. This is a special custom class that does not extend any part of the activity or android environment whatsoever. Because this class never really gets instantiated , it's mostly referred to on a static level. It's still important for me because I hold a number of static enums that are vital to the application's flow. Here's the issue: Because the class doesn't extend the android activity life

Getting resources from non-activity classes

点点圈 提交于 2019-12-25 04:20:35
问题 I have a special class that holds static info and makes calculations based on special circumstances. This is a special custom class that does not extend any part of the activity or android environment whatsoever. Because this class never really gets instantiated , it's mostly referred to on a static level. It's still important for me because I hold a number of static enums that are vital to the application's flow. Here's the issue: Because the class doesn't extend the android activity life