I am trying to simply find a device with mDNS, save the IP and kill the "Network Service Discovery"(NSD) once done.
NSD and AsyncTask are in conflict here.
Getting the IP with NSD works but if the AsyncTask is not static it warns of Leaks.
If AsyncTask is static, NSD says from onPostExecute();
Non-static field 'mNsdManager' cannot be referenced from a static context
NSD is still killed from onDestroy if the App exits if i make the AsyncTask static but have to comment out mNsdManager.stopServiceDiscovery( mDiscoveryListener); in onPostExecute() to do it.
With NSD I get the IP in 5-15 seconds but NSD stays seriously busy if i can't kill it if AsyncTask is static.
If i satisfy AsyncTask by making it static mNsdManager complains:
Non-static field 'mNsdManager' cannot be referenced from a static context
The only way i can compile is make AsyncTask non-static and accept the possible leaks -OR- leave AsyncTask static and comment out the KILL line in onPostExecute().
2 ERRORS marked in the code below.
With Android Event based AsyncTask seems the best way, but is it the right way?
How can I kill mNsdManager and still make AsyncTask static to block leaks?
package com.fib.onacci.fibonacci;
private static final String TAG = "CLOCK : ";
private TextView mIP_address;
// NSD members, Allows app to discover the "fibonacci.local"
// Reference:
// http://developer.android.com/training/connect-devices-wirelessly/nsd.html
private static NsdManager mNsdManager;
private static NsdManager.DiscoveryListener mDiscoveryListener;
private NsdManager.ResolveListener mResolveListener;
private NsdServiceInfo mServiceInfo;
public String mRPiAddress;
public static String IPaddress ="-"; // something to look for change
private static final String SERVICE_TYPE = "_http._tcp.";
public class MainActivity extends AppCompatActivity {
private static final String TAG = "CLOCK: ";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getIPtoTextView().execute(); // start the AsyncTask
// setup nDNS and find IP
mRPiAddress = "";
IPaddress = "-";
mNsdManager = (NsdManager)(getApplicationContext().getSystemService(Context.NSD_SERVICE));
initializeResolveListener();
initializeDiscoveryListener();
mNsdManager.discoverServices( SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
} // END onCreate
// NSD start - Network discovery
ERROR This AsyncTask class should be static or leaks might occur
A static field will leak contexts.
private static class getIPtoTextView extends AsyncTask {
/** part of nDiscovery - find clock and kill discovery service
* `doInBackground` is run on a separate, background thread
*/
#Override
protected Void doInBackground(Void... params) {
String mloop = IPaddress;
while ( mloop.equals("-")) {
mloop = IPaddress;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i( TAG, "doInBackground - IP Found - " + mloop );
return null;
}
/** part of nDiscovery
* `onPostExecute` is run after `doInBackground`, and it's
* run on the main/ui thread, so you it's safe to update ui
* components from it. (this is the correct way to update ui
* components.)
*/
#Override
protected void onPostExecute(Void param) {
Log.i( TAG, "onPostExecute - IP Found - " + IPaddress );
TextView IP_Window = findViewById(R.id.IP_address);
IP_Window.setText( IPaddress); // post IP address to TextView
ERROR Non-static field 'mNsdManager' cannot be referenced from a static context
mNsdManager.stopServiceDiscovery( mDiscoveryListener); // kill mDiscoveryListener
}
} // end asyncTask class
private void initializeDiscoveryListener() {
mDiscoveryListener = new NsdManager.DiscoveryListener() { // Listener
#Override
public void onDiscoveryStarted(String regType) {
}
#Override
public void onServiceFound(NsdServiceInfo service) { // service found!
String name = service.getServiceName();
String type = service.getServiceType();
if ( type.equals(SERVICE_TYPE) && name.contains("Fibonacci")) {
Log.i( TAG, "\n\tNSD Service Found # ' " + name + "'");
mNsdManager.resolveService(service, mResolveListener);
}
}
#Override
public void onServiceLost(NsdServiceInfo service) {
}
#Override
public void onDiscoveryStopped(String serviceType) {
}
#Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
}
#Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
}
};
}
private void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener(){
#Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.i( TAG, "\n\t\tNSD Resolve failed " + errorCode + "\n\n" );
}
#Override
public void onServiceResolved( NsdServiceInfo serviceInfo ) {
mServiceInfo = serviceInfo;
InetAddress host = mServiceInfo.getHost();
IPaddress = host.getHostAddress();
mRPiAddress = IPaddress;
Log.i( TAG, "\n\t\tNSD Resolved address = " + IPaddress + "\n\n" );
}
};
}
#Override
protected void onPause() {
super.onPause();
if ( mDiscoveryListener != null) {
mNsdManager.stopServiceDiscovery( mDiscoveryListener);
}
}
#Override
protected void onResume() {
super.onResume();
if ( mDiscoveryListener != null) {
mIP_address.setText( R.string.searching ); // TextView - Searching -
try {
Thread.sleep( 1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
initializeDiscoveryListener();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mNsdManager.stopServiceDiscovery( mDiscoveryListener);
}
// NSD end //
}
a rewrite was needed because i could not make class getIPtoTextView static without too many errors.
Once i could make class getIPtoTextView static , i moved
mNsdManager.stopServiceDiscovery( mDiscoveryListener); to onPause() and onDestroy() all worked fine.
Related
I would like to implement the library PocketSphinx in my Android project but I fail with it since nothing happens. It doesn't work and I don't get any errors.
This is how I tried it:
Added pocketsphinx-android-5prealpha-release.aar to /app/libs
Added assets.xml to /app
Aded the following to /app/build.gradle:
ant.importBuild 'assets.xml'
preBuild.dependsOn(list, checksum)
clean.dependsOn(clean_assets)
Added sync (with all sub-files) into /app/assets
Cloned the following repos into my root-directory:
git clone https://github.com/cmusphinx/sphinxbase
git clone https://github.com/cmusphinx/pocketsphinx
git clone https://github.com/cmusphinx/pocketsphinx-android
Executed gradle build
This is how my code looks like:
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import ch.yourclick.kitt.R;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import edu.cmu.pocketsphinx.SpeechRecognizer;
import edu.cmu.pocketsphinx.SpeechRecognizerSetup;
public class SttService extends Service implements RecognitionListener {
private static final String TAG = "SstService";
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
private static final String FORECAST_SEARCH = "forecast";
private static final String DIGITS_SEARCH = "digits";
private static final String PHONE_SEARCH = "phones";
private static final String MENU_SEARCH = "menu";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "oh mighty computer";
/* Used to handle permission request */
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;
public SttService() {
// Prepare the data for UI
captions = new HashMap<>();
captions.put(KWS_SEARCH, R.string.kws_caption);
captions.put(MENU_SEARCH, R.string.menu_caption);
captions.put(DIGITS_SEARCH, R.string.digits_caption);
captions.put(PHONE_SEARCH, R.string.phone_caption);
captions.put(FORECAST_SEARCH, R.string.forecast_caption);
Log.e(TAG, "SttService: Preparing the recognition");
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new SetupTask(this).execute();
}
private static class SetupTask extends AsyncTask<Void, Void, Exception> {
WeakReference<SttService> activityReference;
SetupTask(SttService activity) {
this.activityReference = new WeakReference<>(activity);
}
#Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(activityReference.get());
File assetDir = assets.syncAssets();
activityReference.get().setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
#Override
protected void onPostExecute(Exception result) {
if (result != null) {
Log.e(TAG, "onPostExecute: Failed to init recognizer " + result);
} else {
activityReference.get().switchSearch(KWS_SEARCH);
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* In partial result we get quick updates about current hypothesis. In
* keyword spotting mode we can react here, in other modes we need to wait
* for final result in onResult.
*/
#Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
if (text.equals(KEYPHRASE))
switchSearch(MENU_SEARCH);
else if (text.equals(DIGITS_SEARCH))
switchSearch(DIGITS_SEARCH);
else if (text.equals(PHONE_SEARCH))
switchSearch(PHONE_SEARCH);
else if (text.equals(FORECAST_SEARCH))
switchSearch(FORECAST_SEARCH);
else
Log.e(TAG, "onPartialResult: " + text);
}
/**
* This callback is called when we stop the recognizer.
*/
#Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.e(TAG, "onResult: " + text);
}
}
#Override
public void onBeginningOfSpeech() {
}
/**
* We stop recognizer here to get a final result
*/
#Override
public void onEndOfSpeech() {
if (!recognizer.getSearchName().equals(KWS_SEARCH))
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
if (searchName.equals(KWS_SEARCH))
recognizer.startListening(searchName);
else
recognizer.startListening(searchName, 10000);
String caption = getResources().getString(captions.get(searchName));
Log.e(TAG, "switchSearch: "+ caption);
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.getRecognizer();
recognizer.addListener(this);
/* In your application you might not need to add all those searches.
They are added here for demonstration. You can leave just one.
*/
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
// Create grammar-based search for selection between demos
File menuGrammar = new File(assetsDir, "menu.gram");
recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
// Create grammar-based search for digit recognition
File digitsGrammar = new File(assetsDir, "digits.gram");
recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
// Create language model search
File languageModel = new File(assetsDir, "weather.dmp");
recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);
// Phonetic search
File phoneticModel = new File(assetsDir, "en-phone.dmp");
recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
}
#Override
public void onError(Exception error) {
Log.e(TAG, "onError: " + error.getMessage());
}
#Override
public void onTimeout() {
switchSearch(KWS_SEARCH);
}
}
My code is almost the same as pocketsphinx-android-demo. The only differences are that I am doing this in a service class, instead of an Activity and I am not asking the user for microphone permission since I do that in the MainActity already. Well, my code has some warnings but no errors.
When I run my app, I get this message (see the full stack trace):
E/SstService: switchSearch: To start demonstration say "oh mighty
computer".
But when I say "oh mighty computer" (or anything else), nothing happens. I don't even get an error. So I have no idea where I am stuck and what I am doing wrong.
If there is someone familiar with that library, any help will be appreciated!
Inside the onConsentFormLoaded method it says for “variable ‘form’ is accessed from within inner class must be declared final” but then when I declare it final it then shows a error “variable ‘form’ might not have been initialized” I tried declaring 'private static ConsentForm form" at the top of my class but then it gives a error saying about placing android context classes inside a static field will result in a memory leak so im not sure where to go from here ?
public static void settings()
{
final AppActivity activity = ((AppActivity) Cocos2dxHelper.getActivity());
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
URL privacyUrl = null;
try {
// TODO: Replace with your app’s privacy policy URL.
privacyUrl = new URL(“https://privacy-policy”);
} catch (MalformedURLException e) {
e.printStackTrace();
}
ConsentForm form = new ConsentForm.Builder(getContext(), privacyUrl).withListener(new ConsentFormListener() {
#Override
public void onConsentFormLoaded() {
// Consent form loaded successfully.
Log.i(“consent”, “consent loaded”);
form.show();
}
#Override
public void onConsentFormOpened() {
// Consent form was displayed.
}
#Override
public void onConsentFormClosed(ConsentStatus consentStatus, Boolean userPrefersAdFree) {
}
#Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
Log.i(“consent”, errorDescription);
}
})
.withPersonalizedAdsOption().withNonPersonalizedAdsOption().build();
form.load();
}
});
}
I've created a class to download raw data from url(api): GetRawData - GitHub
I extend the class:
GetTVShowDetailsJsonData - GitHub
So, my problem is, I call GetTVShowDetailsJsonData.java on my activity
public class ProcessTVShowsDetails extends GetTVShowDetailsJsonData {
private ProgressDialog progress;
public ProcessTVShowsDetails(TVShow show) {
super(show, TVShowDetailsActivity.this);
}
public void execute() {
// Start loading dialog
progress = ProgressDialog.show(TVShowDetailsActivity.this, "Aguarde...", "Estamos carregando os dados da série.", true);
// Start process data (download and get)
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadJsonData {
protected void onPostExecute(String webData) {
super.onPostExecute(webData);
mTVShowDetails = getTVShowsDetails();
bindParams();
// Close loading dialog.
if (progress.isShowing()) progress.dismiss();
}
}
}
But inside this call, I want to call another download another JSON from other url, using GetSeasonJsonData.java, is the same GetTVShowDetailsJsonData.java do, but for another PARSE.I need to wait the task being completed or maybe do in synchronous way, to add the result from GetSeasonJsonData.java inside the first result from GetTvShowDetailsJsonData. How can I do that?
Just a note, i need to run more than one time, and I already try this, but doesn't work:
// Process and execute data into recycler view
public class ProcessTVShowsDetails extends GetTVShowDetailsJsonData {
private ProgressDialog progress;
public ProcessTVShowsDetails(TVShow show) {
super(show, TVShowDetailsActivity.this);
}
public void execute() {
// Start loading dialog
progress = ProgressDialog.show(TVShowDetailsActivity.this, "Aguarde...", "Estamos carregando os dados da série.", true);
// Start process data (download and get)
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadJsonData {
protected void onPostExecute(String webData) {
super.onPostExecute(webData);
mTVShowDetails = getTVShowsDetails();
//Process SeasonData
for(int seasonNumber = 1; seasonNumber <= mTVShowDetails.getNumberOfSeasons(); seasonNumber++) {
ProcessSeason processSeason = new ProcessSeason(mTVShowDetails.getId(), seasonNumber);
processSeason.execute();
}
bindParams();
// Close loading dialog.
if (progress.isShowing()) progress.dismiss();
}
}
}
// Process Season Data
public class ProcessSeason extends GetTVShowSeasonJsonData {
public ProcessSeason(int showId, int serieNumber) {
super(showId, serieNumber, TVShowDetailsActivity.this);
}
public void execute() {
// Start process data (download and get)
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadJsonData {
protected void onPostExecute(String webData) {
super.onPostExecute(webData);
}
}
}
My ContentObserver for observing the history in the browser is not being called. I don't understand why it isn't. I'm not doing anything different or bizarre, I'm following the API specs exactly, but to no avail! Below is my code:
In my service:
public class MonitorService extends Service {
//some global variables declared here
private ContentObserver historyObserver, searchObserver, chromeObserver;
public void onCreate() {
super.onCreate();
isRunning = false;
this.preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//this.historyObserver = new HistoryObserver();
this.historyObserver = new HistoryObserver(new Handler());
this.searchObserver = new HistoryObserver(new Handler());
this.chromeObserver = new HistoryObserver(new Handler());
getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://com.android.chrome.browser/history"), false, this.chromeObserver);
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Browser.BOOKMARKS_URI, false, this.historyObserver);
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Browser.SEARCHES_URI, false, this.searchObserver);
}
//Other required methods in class
}//end of class
Then in my HistoryObserver Class we have:
public class HistoryObserver extends ContentObserver {
public final String TAG = "HistoryObserver";
public HistoryObserver(Handler handler) {
super(handler);
Log.d(TAG, "Creating new HistoryObserver");
}
public HistoryObserver() {
super(null);
Log.d(TAG, "Creating a new HistoryObserver without a Handler");
}
#Override
public boolean deliverSelfNotifications() {
Log.d(TAG, "delivering self notifications");
return true;
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(TAG, "onChange without uri: " + selfChange);
//onChange(selfChange, null);
}
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Log.d(TAG, "onChange: " + selfChange + "\t " + uri.toString());
}
}
Like I said there is nothing special or unique about this implementation. Yet, when I go go to a new website or search for something in Chrome, the onChange method is never fired.
I figured out the problem. The /history content provider isn't an observable. The observables come through the /bookmark uri. Once I discovered that, things got working very quickly and very well.
I'm getting familiar with Android framework and Java and wanted to create a general "NetworkHelper" class which would handle most of the networking code enabling me to just call web-pages from it.
I followed this article from the developer.android.com to create my networking class: http://developer.android.com/training/basics/network-ops/connecting.html
Code:
package com.example.androidapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;
/**
* #author tuomas
* This class provides basic helper functions and features for network communication.
*/
public class NetworkHelper
{
private Context mContext;
public NetworkHelper(Context mContext)
{
//get context
this.mContext = mContext;
}
/**
* Checks if the network connection is available.
*/
public boolean checkConnection()
{
//checks if the network connection exists and works as should be
ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
//network connection works
Log.v("log", "Network connection works");
return true;
}
else
{
//network connection won't work
Log.v("log", "Network connection won't work");
return false;
}
}
public void downloadUrl(String stringUrl)
{
new DownloadWebpageTask().execute(stringUrl);
}
//actual code to handle download
private class DownloadWebpageTask extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... urls)
{
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException
{
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("log", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException
{
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
// onPostExecute displays the results of the AsyncTask.
#Override
protected void onPostExecute(String result)
{
//textView.setText(result);
Log.v("log", result);
}
}
}
In my activity class I use the class this way:
connHelper = new NetworkHelper(this);
...
if (connHelper.checkConnection())
{
//connection ok, download the webpage from provided url
connHelper.downloadUrl(stringUrl);
}
Problem I'm having is that I should somehow make a callback back to the activity and it should be definable in "downloadUrl()" function. For example when download finishes, public void "handleWebpage(String data)" function in activity is called with loaded string as its parameter.
I did some googling and found that I should somehow use interfaces to achieve this functionality. After reviewing few similar stackoverflow questions/answers I didn't get it working and I'm not sure if I understood interfaces properly: How do I pass method as a parameter in Java? To be honest using the anonymous classes is new for me and I'm not really sure where or how I should apply the example code snippets in the mentioned thread.
So my question is how I could pass the callback function to my network class and call it after download finishes? Where the interface declaration goes, implements keyword and so on?
Please note that I'm beginner with Java (have other programming background though) so I'd appreciate a throughout explanation :) Thank you!
Use a callback interface or an abstract class with abstract callback methods.
Callback interface example:
public class SampleActivity extends Activity {
//define callback interface
interface MyCallbackInterface {
void onDownloadFinished(String result);
}
//your method slightly modified to take callback into account
public void downloadUrl(String stringUrl, MyCallbackInterface callback) {
new DownloadWebpageTask(callback).execute(stringUrl);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//example to modified downloadUrl method
downloadUrl("http://google.com", new MyCallbackInterface() {
#Override
public void onDownloadFinished(String result) {
// Do something when download finished
}
});
}
//your async task class
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
final MyCallbackInterface callback;
DownloadWebpageTask(MyCallbackInterface callback) {
this.callback = callback;
}
#Override
protected void onPostExecute(String result) {
callback.onDownloadFinished(result);
}
//except for this leave your code for this class untouched...
}
}
The second option is even more concise. You do not even have to define an abstract method for "onDownloaded event" as onPostExecute does exactly what is needed. Simply extend your DownloadWebpageTask with an anonymous inline class inside your downloadUrl method.
//your method slightly modified to take callback into account
public void downloadUrl(String stringUrl, final MyCallbackInterface callback) {
new DownloadWebpageTask() {
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
callback.onDownloadFinished(result);
}
}.execute(stringUrl);
}
//...
NO interface, NO lib, NO Java 8 needed!
Just using Callable<V> from java.util.concurrent
public static void superMethod(String simpleParam, Callable<Void> methodParam) {
//your logic code [...]
//call methodParam
try {
methodParam.call();
} catch (Exception e) {
e.printStackTrace();
}
}
How to use it:
superMethod("Hello world", new Callable<Void>() {
public Void call() {
myParamMethod();
return null;
}
}
);
Where myParamMethod() is our passed method as parameter (in this case methodParam).
Yes, an interface is the best way IMHO. For example, GWT uses the command pattern with an interface like this:
public interface Command{
void execute();
}
In this way, you can pass function from a method to another
public void foo(Command cmd){
...
cmd.execute();
}
public void bar(){
foo(new Command(){
void execute(){
//do something
}
});
}
The out of the box solution is that this is not possible in Java. Java does not accept Higher-order functions. It can be achieved though by some "tricks". Normally the interface is the one used as you saw. Please take a look here for further information. You can also use reflection to achieve it, but this is error prone.
Using Interfaces may be the best way in Java Coding Architecture.
But, passing a Runnable object could work as well, and it would be much more practical and flexible, I think.
SomeProcess sp;
public void initSomeProcess(Runnable callbackProcessOnFailed) {
final Runnable runOnFailed = callbackProcessOnFailed;
sp = new SomeProcess();
sp.settingSomeVars = someVars;
sp.setProcessListener = new SomeProcessListener() {
public void OnDone() {
Log.d(TAG,"done");
}
public void OnFailed(){
Log.d(TAG,"failed");
//call callback if it is set
if (runOnFailed!=null) {
Handler h = new Handler();
h.post(runOnFailed);
}
}
};
}
/****/
initSomeProcess(new Runnable() {
#Override
public void run() {
/* callback routines here */
}
});
Reflection is never a good idea since it's harder to read and debug, but if you are 100% sure what you're doing, you can simply call something like set_method(R.id.button_profile_edit, "toggle_edit") to attach a method to a view. This is useful in fragment, but again, some people would consider it as anti-pattern so be warned.
public void set_method(int id, final String a_method)
{
set_listener(id, new View.OnClickListener() {
public void onClick(View v) {
try {
Method method = fragment.getClass().getMethod(a_method, null);
method.invoke(fragment, null);
} catch (Exception e) {
Debug.log_exception(e, "METHOD");
}
}
});
}
public void set_listener(int id, View.OnClickListener listener)
{
if (root == null) {
Debug.log("WARNING fragment", "root is null - listener not set");
return;
}
View view = root.findViewById(id);
view.setOnClickListener(listener);
}
Interface callback support generic type.
In Callbackable.java
public interface Callbackable<T> {
void call(T obj);
}
How to use:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doSomeNetworkAction(new Callbackable<Integer>() {
#Override
public void call(Integer obj) {
System.out.println("have received: " + obj + " from network");
}
});
}
// You can change Integer to String or to any model class like Customer, Profile, Address...
public void doSomeNetworkAction(Callbackable<Integer> callback) {
// acb xyz...
callback.call(666);
}
}