Cristian Annese Cristian Annese - 4 months ago 23
Java Question

How to call a public void to run automatically when my app iniciates

I need to call the following public void automatically as soon as the app gets open:

public void GetIMEI(){
String deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
Toast.makeText(MainActivity.this, "IMEI: " + deviceId, Toast.LENGTH_SHORT).show();
}


How can I do that?

Update: This is the main activity of my app

MainActivity.JAVA:

package ar.com.reservo.reservo;

//here go all the imports

public class MainActivity extends Activity {

/* URL saved to be loaded after fb login */
private static final String target_url="http://chapatelo.hol.es/";
private static final String target_url_prefix="chapatelo.hol.es";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private RelativeLayout mContainer;
private long mLastBackPressTime = 0;
private Toast mToast;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.activity_main_webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (RelativeLayout) findViewById(R.id.webview_relative);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setVerticalScrollBarEnabled(true);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mWebview.getWindowToken(), 0);

mWebview.setWebViewClient(new UriWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.splashLoading1).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});

mWebview.setWebChromeClient(new UriChromeClient(){

// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);

}

// For Android 3.0+
public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}

//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );

}
});
mWebview.loadUrl(target_url);

mContext=this.getApplicationContext();
}


private class UriWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
//Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null){
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}

if(host.equals("m.facebook.com") || host.equals("www.facebook.com")){
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}

class UriChromeClient extends WebChromeClient {

@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();

return true;
}

@Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}

@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}


public void GetIMEI(){
String deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
Toast.makeText(MainActivity.this, "IMEI: " + deviceId, Toast.LENGTH_SHORT).show();
}

/*@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

if(keyCode == KeyEvent.KEYCODE_BACK){
mWebview.loadUrl("javascript:()");
}

return super.onKeyDown(keyCode, event);
}*/


@Override
public void onBackPressed(){
/* if(mWebview.canGoBack()){
mWebview.goBack();
}else{
Toast.makeText(MainActivity.this, "Has salido de 'Contacto'", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}*/
Toast.makeText(MainActivity.this, "Saliste de 'Chapátelo'. ¡Nos vemos la próxima vez!", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}


I hope it is enough information to make clear how my code is and to help you find out how to call that public void specifically for my case.

Answer

Put it in the onCreate method of your activity:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GetIMEI();
    }

    //your GetIMEI method here
}
Comments