21 Ocak 2017 Cumartesi

internet kontrolu

public boolean networkConnection() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
        if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
            return true;
        }else{
            return false;
        }
}
Bir WebView uygulamasında bu kodu basitçe nasıl kullanabilirsiniz,
if(networkConnection()){
   webview.loadUrl("http://serifgungor.com");
}else{
   Toast.makeText(getApplicationContext(), "Bağlantı problemi oluştu", Toast.LENGTH_LONG).show();
   finish();
}
AndroidManifest.xml içerisine Permission olarak ekleyiniz;
  • uses-permission android:name=android.permission.INTERNET
  • uses-permission android:name=android.permission.ACCESS_NETWORK_STATE

10 Ocak 2017 Salı

android instert

https://www.youtube.com/watch?v=H-SE1m_A-SA&t=88s

ilk kez çalıştırma

SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun)
{
    // Code to run once
    SharedPreferences.Editor editor = wmbPreference.edit();
    editor.putBoolean("FIRSTRUN", false);
    editor.commit();
}


SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
    // run your one time code here
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}


public void onCreate(Bundle savedInstanceState) {
  // don't forget to call super method.
  super.onCreate(savedInstanceState);

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  if (!prefs.getBoolean("firstTime", false)) {
    // <---- run your one time code here
    databaseSetup();

    // mark first time has runned.
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
  }
}
Don't forget to put activity declaration in manifest, as well as it's intentfilters (action = MAIN, category = LAUNCHER).
If you have more than one activity and you don't want to duplicate your startup logic you can just put your initialization logic in Application instance, that is created before all activities (and other components, such as services, broadcast recievers, content providers).
Just create class like that:
public class App extends Application {

  @Override
  public void onCreate() {
    super.onCreate();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!prefs.getBoolean("firstTime", false)) {
      // <---- run your one time code here
      databaseSetup();

      // mark first time has runned.
      SharedPreferences.Editor editor = prefs.edit();
      editor.putBoolean("firstTime", true);
      editor.commit();
    }
}