Android: call method of overrided class from library - android

In this example I have an app which is playing mp3 songs, but there are different license checks by companies.
So in my library I have 3 files:
public interface UserCheckerInterface {
public void appIsEnabled(boolean result);
}
public class UserChecker {
public static void appisEnabled(final UserCheckerInterface userCheckerInterface) {
userCheckerInterface.appIsEnabled(true);
}
}
public class MainActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
....
UserChecker.appisEnabled(new UserCheckerInterface(
#Override
public void appisEnabled(final boolean result) {
Toast.makeText(getApplicationContext(), "" + result, 0).show();
}
));
}
}
I would like override the UserChecker.appisEnabled method in my app which is using this library, but I don't know how.

I am not sure whether I have understood your question, if I did, than you simply have to implement your interface by writing
public class UserChecker implements UserCheckerInterface{
#Override
public static void appisEnabled(final UserCheckerInterface userCheckerInterface) {
userCheckerInterface.appIsEnabled(true);
}
}
Once you do that, then the IDE will show you an error IF you have not implemented the method; which is not the case in this scenario.

Related

Dagger listener/interface injection

Hello everyone I've been struggling to understand how to inject a listener to a main activtity with Dagger2, I wonder if what I'm trying to do is possible or even a right move with dagger or should I just let it like it is right now I have read that I need to create another class with the implementation of that interface but is not possible(or recommended) to inject on the mainactivity?, thanks in advance to anyone who can help me, I have everything in short as follows:
//////////////////////////////////////MainActivity.class//////////////////////////////////////
public class MainActivity extends AppCompatActivity implements CustomListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this is the object I want to inject in Dagger
LongProcess longProcess = new LongProcess(this);
longProcess.longRunningProcess();
}
#Override
public void onProcessStarted() {
Log.i(TAG, "onProcessStarted: CALLBACK!");
}
#Override
public void onProcessFailed() {
Log.e(TAG, "onProcessFailed: CALLBACK!");
}}
//////////////////////////////////////LongProcess.class//////////////////////////////////////
public class LongProcess {
private CustomListener customListener;
public LongProcess(CustomListener customListener) {
this.customListener = customListener;
}
public void longRunningProcess() {
try {
//some long process started...
customListener.onProcessStarted();
} catch (Exception e) {
//some long process failed...
customListener.onProcessFailed();
}
}
}
//////////////////////////////////////interface.java//////////////////////////////////////
public interface CustomListener {
void onProcessStarted();
void onProcessFailed();
}
You can take a look at Assisted Injection for this use case: https://dagger.dev/dev-guide/assisted-injection

Is it possible to create a base dialogfragment that binds strings using butterknife

I'm trying to build a dialog that shows a list of items fetched from the network. Because I know that this isn't the only time that I will need this type of dialog I would like to build a base dialog (NetworkDialog) that handles the different errors that can happen when trying to fetch data from the network (ie. noServerResponse, failedToConnectToHost etc).
For this I would like to be able to use Butterknife to bind the string resources that are used for the different error messages, but I'm not sure if that will be possible.
Would something like this work?
public abstract class NetworkDialog extends BaseDialog implements NetworkDialogView {
#BindString(R.string.global_network_error_internal_server_error)
String mInternalServerErrorString;
#BindString(R.string.global_network_error_no_server_response)
String mNoServerResponseString;
#BindString(R.string.global_network_error_no_network)
String mNoNetworkString;
#BindString(R.string.global_network_error_failed_to_connect_to_host)
String mFailedToConnectToHost;
private Unbinder mUnbinder;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Timber.d("onCreateDialog()");
Timber.d("Binding view using ButterKnife.");
mUnbinder = ButterKnife.bind(this, getView());
Timber.d("View bound");
return super.onCreateDialog(savedInstanceState);
}
#Override
public void onDestroyView() {
Timber.d("onDestroyView() called.");
super.onDestroyView();
mUnbinder.unbind();
}
#Override
public void setNoNetworkError() {
showError(mNoNetworkString);
}
#Override
public void setFailedToConnectToHostError() {
showError(mFailedToConnectToHost);
}
#Override
public void setNoServerResonseError() {
showError(mNoServerResponseString);
}
#Override
public void setInternalServerError() {
showError(mInternalServerErrorString);
}
}

Call a method in the calling class on API success in android

I use the following class to make an API call in android using Retrofit
public Class Checkin {
public static void checkinViaApi(CheckinSendModel checkinSendModel) {
final ApiHandler apiHandler = new ApiHandler();
apiHandler.setApiResponseListener(new ApiResponseListener() {
#Override
public void onApiResponse(ApiResponseModel apiResponse) {
Log.i("CheckedIn","true");
}
#Override
public void onApiException(Error error) {
Log.i("fail",error.getErrorMessage());
}
});
List<CheckinSendModel> checkinSendModelList = new ArrayList<CheckinSendModel>();
checkinSendModelList.add(checkinSendModel);
Call<ApiResponseModel> request = RetrofitRestClient.getInstance().checkinToMainEvent(checkinSendModelList,Constant.API_KEY);
apiHandler.getData(request);
}
}
I call that method as follows:
Checkin.checkinViaApi(checkinSendModelObject);
Now, when the API call is successful, I want to execute a function checkedInSuccessfully() in the class from where I make the call. How can I do it?
Thanks in advance
Pass in the response interface.
public class Checkin {
public static void checkinViaApi(CheckinSendModel checkinSendModel, ApiResponseListener listener) {
final ApiHandler apiHandler = new ApiHandler();
apiHandler.setApiResponseListener(listener);
Other class - Call that method
CheckinSendModel model;
Checkin.checkinViaApi(model, new ApiResponseListener() {
#Override
public void onApiResponse(ApiResponseModel apiResponse) {
Log.i("CheckedIn","true");
checkedInSuccessfully();
}
#Override
public void onApiException(Error error) {
Log.i("fail",error.getErrorMessage());
}
);
Interface is your handy man. Create an interface like below.
Interface CheckInListener {
void onCheckIn();
}
Change the checkinViaApi() to below signature.
public static void checkinViaApi(CheckinSendModel checkinSendModel, CheckinListener listener) {
#Override
public void onApiResponse(ApiResponseModel apiResponse) {
Log.i("CheckedIn","true");
listener.onCheckIn();
}
}
When you call the above function you can provide an instance of the interface.
Checkin.checkinViaApi(checkinSendModelObject, new CheckInListener() {
#Override
void onCheckIn() {
//Do your action here
}
});

Gomobile android using callbacks

I have an library written using go mobile and it should has only one callback but when trying implement it, I get two additional methods.
#Override
public Seq.Ref ref() {
return null;
}
#Override
public void call(int i, Seq seq, Seq seq1) {}
Question is, which is right way to implement callback from go on Android Activity?
Right now i have next:
public class MainActivity extends Activity implements implements Mobile.Callback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Mobile.Client client = Mobile.New("192.168.2.1", 9000, this);
try {
client.Connect();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void OnMessage(String s) {
Log.e("GO", s);
}
#Override
public Seq.Ref ref() {
return null;
}
#Override
public void call(int i, Seq seq, Seq seq1) {
}
}
Connection is established successfully but on callback to activity i getting:
panic: runtime error: invalid memory address or nil pointer dereference
If someone can help I'll be really appreciate.
What is the Go source you are binding? (The package mobile and Callback interface)
For passing the Java class that implements Go interface type, see the section "Passing target language objects to Go" of
https://godoc.org/golang.org/x/mobile/cmd/gobind
Basically, the generated Java interface type is not meant to be used directly. Instead, the Java class should extends the generated Java interface's Stub class.
Use Mobile.Callback.Stub instead of Mobile.Callback for android
...
Mobile.Client client = Mobile.New("192.168.2.1", 9000, new Callbacks());
...
class Callbacks extends Mobile.Callback.Stub {
#Override
public void OnMessage(String s) {
....
}
}

Integrating Google Analytis to android app

https://developers.google.com/analytics/devguides/collection/android/v2/#manifest "Add the send methods to the onStart() and onStop() methods of each of your Activities as in the following example:"
Here is the question, I have no onStart and onStop methods in my main class. Should I put that piece of code in all of my methods? Or only in specific ones? I have a lot of methods in my class (probably should do something about it...):
package com.something.smth;
import something.com;
#SuppressLint("DefaultLocale")
public class Main extends Activity implements View.OnClickListener {
EditText input;
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
something
}
private void whatToDo() {
something
}
#Override
public void onClick(View v) {
something
}
private void prefdata() {
something
}
private void printAll(int i, int examNumb) {
something
}
private void printOutFirst(String lesson, String type, int monthD,
int dayD, int hourD) {
something
}
private void printOutSecond(int monthD, int dayD, int hourD) {
something
}
private void timeleft(int mDate, int dDate, int hDate) {
something
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
something
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
something
}
}
Also, should I put that piece of code in all of my class'es or only in my main (above) class?
Thanks in advance.
You definitely should create those methods and put the appropriate GA calls in there. Another option would be to build a base class and extend it so you're not duplicating code. I have some other tricks outlined here:
http://www.aydabtudev.com/2011/03/google-analytics-tricks-for-android.html

Categories

Resources