how to getSystemService within a ContentProvider in android? - android

I have class structure like this:
public class DHTContentProvider extends ContentProvider {
private Networking networking = new Networking();
public boolean onCreate() {
networking.init();
}
public class Networking extends Service {
public void init() {
TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
}
}
}
Networking is a subclass of DHTContentProvider.
When I run it always NullPointerException. Anyone has idea?

Call getContext() to retrieve a Context, then call getSystemService() on that Context.

Maybe you should delete them "Context.".

Related

Mock activity class member before onCreate

I am trying to test an activity using robolectric 3.3.2.
Want to mock and activity's member initialization as the direct call results in NPE.
ActivityController<MyActivity> activityController =
Robolectric.buildActivity(MyActivity.class);
mTestActivity = activityController.get();
Mockito.when(mTestActivity.getCountry()).thenReturn("xxxx");
activityController.setup();
Tried out above code, but the setup.() (oncreate) ignores the mock of
getCountry method and invokes the definition from activity.
Is there a way to achieve this?
It will not work like this even if you use spies (#Spy, Mockito.spy()).
You should use stub:
public class MyActivityTest{
public static class StubMyActivity extends MyActivity {
Country getCountry() {
return someSpecialCountry;
}
}
#Before
public void setUp(){
ActivityController<StubMyActivity> activityController =
Robolectric.buildActivity(StubMyActivity.class);
mTestActivity = activityController.setup().get();
}
}

getSystemService from non Activity class

I need to get LAC and Cid codes in my Android application. However, I need to do it inside a not-Activity class. The code I found is this:
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
However, the method setSystemService is present only in Activity classes, and I have not found something to send "some sort of activity" to the class.
Is there any way of do so, without activity ?
You can pass context as an argument to constructor of the class. Inside the constructor you can initialize the TelephonyManager.
Example :
In class,
public class MyClass {
private TelephonyManager mTelephonyManager;
public MyClass(Context context) {
mTelephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
}
In Activity,
public class MyActivity extends Activity {
....
//Initializing class
MyClass myClass = new MyClass(this);
..
You just need to pass a Context object to call getSystemService() from it. You can also use an Activity object as Activity extends Context.
Then just call context.getSystemService()

How to acces getSystemService in Application class

I want to access the TELEPHONY_SERVICE system service in the Application class, but my app crashes when I run it.
public class SimpleDhtApplication extends Application {
TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() -4);
final String myPort = String.valueOf((Integer.parseInt(portStr) * 2));
}
I think I am not accessing the context correctly, can somebody help!
Override the onCreate method of the Application class first. Within the onCreate, put the code you have so it would look like:
public class SimpleDhtApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() -4);
final String myPort = String.valueOf((Integer.parseInt(portStr) * 2));
}
}
Update: Watch out for tel.getLine1Number(); it may return null as explained here.

Passing a null context to a database helper

I have a static function that gets called whenever my background service gets a new location. In this function I want to take to coordinates passed in and save them in my database. Can I pass 'null' as the context to create an instance of the database helper or is there a better way to do this. Thanks.
public static void locationHasChanged() {
final wd_DatabaseHelper helper = new wd_DatabaseHelper(null, "myDB.db", null, 1);
}
Probably not. Usually your Database helper extends SQLiteOpenHelper and the context will be used to call the openOrCreateDatabase() or the getDatabasePath(). I can't say for sure without seeing the code of wd_DatabaseHelper but having a null context is never a good idea. See for your self ... Source of SQLiteOpenHelper
since an android Service is a context you can pass "this of the service" into your method
public class MyLocationHelper {
public static void locationHasChanged(Context context) {
final wd_DatabaseHelper helper = new wd_DatabaseHelper(context, "myDB.db", null, 1);
....
}
}
public class MyService extends Service {
private void onLocationHasChanged()
{
MyLocationHelper.locationHasChanged(this);
}
}

why does getContentResolver() within Application cause NullPointerException?

in the following code:
public class ApplicationContext extends Application
{
private static ApplicationContext instance;
public ApplicationContext()
{
instance = this;
final String strID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
}
public static Context getContext()
{
return instance;
}
}
getContentResolver() causes a NullPointerException. Why ?
I find this exception especially confusing because Google states "You get a ContentResolver by calling getContentResolver() from within the implementation of an Activity or other application component"
http://developer.android.com/guide/topics/providers/content-providers.html
Do this when overriding oncreate better than in your constructor. I guess your app doesn't have a context yet.
Actually, here is what I did yesterday for some LVL code :
/** Called when the activity is first created. */
#Override
public void onCreate() {
super.onCreate();
LICENSED_APP_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
}//cons
And it works like a charm...
The "Application" class is not an "Application Component".
http://developer.android.com/guide/topics/fundamentals.html#Components
To solve this problem, my plan is to grab the ANDROID_ID from within a service.

Categories

Resources