I want to add a function into my firebase-messaging librady.The file is not writable.Is it possible somehow to write in it?
No, you can't do that. If you want to add a method to FirebaseMessagingService, make an abstract class that extends FirebaseMessagingService and extend that class in your implementations.
public abstract class BaseMessagingService extends FirebaseMessagingService {
public void yourMethod() {
}
}
public class YourActualMessagingService extends BaseMessagingService {
//...
}
Related
I'm building a library that I use then on my application. The idea is that the library has an Abstract class that extends from IntentService so that the app can create a subclass of this Service and manage the information there.
The problem is that I have declared MyIntentService that extends from the IntentService of the library, declared on the manifest but when I start to run it within the library the service can't be found.
Makes sense that is not found as from the library I don't know the new class I just try to open the one that is abstract from the library.
Any help is welcome, I'm a bit lost.
Library -> Abstract IntentService
public abstract class NotificationReceiver extends IntentService {
public NotificationReceiver() {
super("NotificationReceiver");
}
public final void onHandleIntent(Intent intent) {
L.d("NotificationReceiver","onHandleIntent","NEW MESSAGE");
this.notificationReceived();
}
public abstract void notificationReceived();}
This does not get declared inside the manifest as it's abstract
App -> IntentService that extends the one from the library
public class MyNotificationHandler extends NotificationReceiver {
public MyNotificationHandler() {
super();
}
public MyNotificationHandler(String name) {
super();
}
#Override
public void notificationReceived() {
Log.d("MyNotificationHandler","NEW NOTIFICATION TO DISPLAY");
}}
This is declared in the manifest of the application:
<!-- NOTIFICATIONS -->
<service android:name="com.test.demosdk.MyNotificationHandler" >
</service>
And the IntentService gets started from the library using this:
Intent generalIntent = new Intent(DemoSDKManager.getApplicationContext(),NotificationReceiver.class);
startService(generalIntent);
The above results on an IntentService not found.
I am using a singleton class to store global variables for the entire project. Also, to host some common functions which several classes/Activities may use, such as launching an alertBuilder window. But in order to do that... I need my singleton to extend Activity like this:
public class dataBaseObject extends Activity {
I tried to extend application, but that won't allow me to do this:
View view = context.getLayoutInflater().inflate(layoutType, null);
therefore, can someone tell me if there are any hidden pitfalls of extending Activity for a singleton ?
It doesn't make sense for an Activity class to be a singleton, because instances of Activity are instantiated by the android system.
What you can do is make an abstract class that extends Activity, like this
public abstract class AbstractActivity extends Activity {
public static final int EXAMPLE_CONSTANT = 345;
public final void exampleMethod() {
...
}
// This may not be needed
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
....
}
}
Then you can make all of your activity classes extend AbstractActivity. You do not need to declare an abstract class like this in manifest.xml.
An alternative solution is to make all of your utility methods have a parameter that is an Activity or a Context and pass this to these methods.
I use abstract activity classes in my code to well, abstract away some features from the activity classes.
I'm trying to test the abstract activity classes using Robolectric and the gradle-android-test-plugin using subclasses that extend the abstract class. I can't seem to get it to work though.
Does anyone have any experience in this area and is it even possible ? Basic structure is :
#RunWith(RobolectricGradleTestRunner.class)
public class AbstractActivityTest {
private ActivityTest activity;
#Before
public void setUp() throws Exception {
activity = Robolectric.buildActivity(ActivityTest.class).create().get();
}
private class ActivityTest extends AbstractActivity {
// do something
}
}
Initially, I got the error message the sub class wasn't static so I made it static. Now I get the following two fails:
initializationError FAILED
java.lang.Exception: Test class should have exactly one public constructor
initializationError FAILED
java.lang.Exception: No runnable methods
Any obviously true tests I put in #Test methods succeed.
The first error saying that you added non-default constructor to your test class or changed access level for default one. But as it says junit Test class should have at least one public constructor.
The second one says that at least one method in test class should have #Test annotation (junit 4) or starts with test substring (junit 3).
Yo can doing exactly what you are trying to do: subclass the abstract activity and instance the concrete class.
However, you need to declare the class extending the abstract Activity in it's own public file. If it's a nested class Robolectric will fail to instance it.
I don't know why, though.
I test an abstract activity this way:
1. Creating the abstract avtivity:
public abstract class AbstractActivity extends AppCompatActivity {
public int getNumber() {
return 2;
}
}
2. Creating the test class:
You just need to declare a static nested subclass of your abstract class.
#RunWith(RobolectricTestRunner.class)
public class AbstractActivityTest {
#Test
public void checkNumberReturn() throws Exception {
TestAbstractActivity testAbstractActivity = Robolectric.setupActivity(TestAbstractActivity.class);
assertThat(testAbstractActivity.getNumber(), is(2));
}
public static class TestAbstractActivity extends AbstractActivity {
}
}
I want to use two type asynctaskloader in one FragmentActivity.
class MyLoader1 extends AsyncTaskLoader<String>{}
class MyLoader2 extends AsyncTaskLoader<Integer>{}
I write as follows. but it compile error.
public class MyActivity extends FragmentActivity
implements LoaderCallbacks<String>, LoaderCallbacks<Integer>{}
Please show me answer with easy sample code.
Thanks so much.
As hjpotter92 mentions, this is how Java handles generics. In this case, I would just suggest using anonymous classes as indicated in hjpotter92's link.
public class MyActivity extends FragmentActivity {
private LoaderCallbacks<String> mLoaderCallbackString = new LoaderCallbacks<String>() {
...
};
private LoaderCallbacks<Integer> mLoaderCallbackInteger = new LoaderCallbacks<Integer>() {
...
};
}
Then for each loader, you just pass the correct LoaderCallbacks object
I want to use different AsyncTaskLoaders (different in their return type) in my Activity, what's the best way to implement the callback methods?
This won't work:
public class MyActivity extends Activity implements
LoaderManager.LoaderCallbacks<MyPojo>,
LoaderManager.LoaderCallbacks<MyOtherPojo>
Eclipse says
The interface LoaderCallbacks cannot be implemented more than once with different arguments
So what do I do? My idea is to make the Activity
implements LoaderManager.LoaderCallbacks<Object>
then check in the callback methods what type of object it is but that doesn't seem too elegant. Is there a better way?
What about creating an inner class for each callback?
public class MyClass extends Activity {
private class Callback1 implements LoaderManager.LoaderCallbacks<MyPojo> {
...
}
private class Callback2 implements LoaderManager.LoaderCallbacks<MyOtherPojo> {
...
}
}