android-context

how to use SharedPreferences outside of oncreate?

倖福魔咒の 提交于 2020-01-02 09:54:16
问题 How to use SharedPreferences in a class without oncreate ? I get null pointer when accessing it. public class Ftr extends Activity { SharedPreferences preferences; Context ab=this; public void ft() { preferences = PreferenceManager.getDefaultSharedPreferences(ab); String result = preferences.getString("F",""); } } I'm calling Function ft() from another activity Ftr is just a class not an activity. How can I use SharedPreferences in this condition? 回答1: You can take in Common method or Utils

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

蹲街弑〆低调 提交于 2020-01-02 08:04:33
问题 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. 回答1: 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",

how to call non static method from static method in android

假如想象 提交于 2020-01-02 07:26:45
问题 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()

Build Signed APK failed - Can't find common super class of [android/content/Context]

假如想象 提交于 2019-12-31 02:54:10
问题 This is Android Project. Using Normal Build was successfull, but using signed Key i got this error : Execution failed for task ':transformClassesAndResourcesWithProguardForFlavorRelease'. > java.io.IOException: java.lang.IllegalArgumentException: Can't find common super class of [android/content/Context] (with 2 known super classes) and [com/onesignal/ADMMessageHandler] (with 1 known super classes) Any Idea ? Thanks in advance... 回答1: I think Proguard causes this problem. Add this line to

Call activity method from broadcast receiver android?

狂风中的少年 提交于 2019-12-30 07:48:09
问题 In my application I am sending a port SMS to the mobile. And when the message is recieved I need to perform some task in my activity and update the UI. Manifest Declaration of receiver <receiver android:name="com.vfi.BinarySMSReceiver" > <intent-filter android:priority="10" > <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> <data android:host="*" android:port="9512" android:scheme="sms" /> </intent-filter> </receiver> Receiver class public class BinarySMSReceiver extends

Android ApplicationTestCase using a MockContext

别说谁变了你拦得住时间么 提交于 2019-12-29 07:42:12
问题 I'm new to Android testing and I'm trying to create an ApplicationTestCase using a MockContext (well actually I'm trying to use a Renaming Mock Context). But I keep getting an AssertionFailedError. Here's my very basic code so far: AppTests.java package com.myProject.test; import android.test.ApplicationTestCase; public class AppTests extends ApplicationTestCase<MyApplication> { public AppTests() { super(MyApplication.class); } @Override protected void setUp() throws Exception { final

Android ApplicationTestCase using a MockContext

二次信任 提交于 2019-12-29 07:42:04
问题 I'm new to Android testing and I'm trying to create an ApplicationTestCase using a MockContext (well actually I'm trying to use a Renaming Mock Context). But I keep getting an AssertionFailedError. Here's my very basic code so far: AppTests.java package com.myProject.test; import android.test.ApplicationTestCase; public class AppTests extends ApplicationTestCase<MyApplication> { public AppTests() { super(MyApplication.class); } @Override protected void setUp() throws Exception { final

What is the right way to communicate from a custom View to the Activity in which it resides?

谁说胖子不能爱 提交于 2019-12-28 13:46:10
问题 I have a custom View class that extends Spinner. I'm trying to figure out what the correct way to talk to the Activity that it's embedded in is, when the user makes a selection. I see that the OnItemSelected listener gets a reference to the Adapter, but I'm not clear on whether or not I should be using this adapter and walking up its parent chain somehow, or if I should just talk directly to the context (for some reason that doesn't feel safe, even though I can't think of a way in which it

SharedPreferences application context vs activity context

笑着哭i 提交于 2019-12-28 11:49:10
问题 I am using several SharedPreferences to store data in my app. Some preferences are used in a lot of activites. I know that the SharedPreferences are internally backed by a map for fast read-access and written to sdcard when settings are changed. I wonder which way is better if a sharedpreference is accessed by a lot of activies: Instantiate it in every activity using the activity context. Instantiate it in every activity, but using the application context. Put it in e.g. the Application class

Android : References to a Context and memory leaks

。_饼干妹妹 提交于 2019-12-28 08:10:14
问题 I've read that it is a mistake and a source of memory leaks in Android application to keep a long-lived references to a Context. But I don't understand if it is ok to create a class that looks like this one: public class HelperClass { private Context context; public HelperClass(Context context) { this.context = context; } public void myHelperMethod() { // uses this.context } } And call it from an Activity: public class MyActivity extends Activity { public void onCreate(Bundle