Tuesday, June 30, 2015

Send SMS in android

You can use this to send sms to any number.
Use this in XML
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:onClick="sendsms"
        android:id="@+id/button2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
Use this in Activity Class
 public void sendsms(View view) {
        String phoneNumber = "+880xxxxxxxxxx";
        String message = "Welcome to sms";
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber));
        intent.putExtra("sms_body", message);
        startActivity(intent);
    }
Need SEND_SMS permission.
<uses-permission android:name="android.permission.SEND_SMS" />

Android: Time Loop in milliseconds

You can use timer or thread handler
   timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

               Do your own code
                ///////schedule the timer, after the first 100ms the TimerTask will run every 1000ms
            }
        }, 1000, 1000);
Can Use Another handler
  Handler handler = new Handler();
        handler.postDelayed(
            new Runnable() {
                public void run() {
                    afficher();
                }
            }, 1000L);

How to Customize Toast In Android

To Customize Toast shape and color use this.
Toast toast = Toast.makeText(getApplicationContext(), "You not Subscribe Try again", Toast.LENGTH_LONG);
                            View vieew = toast.getView();
                            //  vieew.setBackgroundColor(Color.parseColor("#BD8BDC"));
                            vieew.setBackgroundResource(R.drawable.textinputborder);
                            toast.setView(vieew);
Also use in R.drawable.textinputborder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#83BB66" />
            <stroke android:width="1dp"
                    android:color="#1B200A" />
            <corners android:radius="20dp" />
        </shape>
    </item>
</selector>

How to (turn off wifi (if on wifi )) and turn on mobile data connection in Android

Use this method to (turn off wifi(if on wifi)) and turn on mobile data connection
  public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean NisConnected = activeNetwork != null && activeNetwork.isConnected();
        if (NisConnected) {
            //  if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                //  wifi.setWifiEnabled(true);//Turn on Wifi
                wifi.setWifiEnabled(false);//Turn off Wifi
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return true;
            else
                return false;
        }
        return false;
    }
after use up method , if you can see internet not connection then use bellow method to turn on Mobile data
 private void setMobileDataEnabled(Context context, boolean enabled) {
        try {
            final ConnectivityManager conman = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final Class conmanClass = Class.forName(conman.getClass().getName());
            final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
            iConnectivityManagerField.setAccessible(true);
            final Object iConnectivityManager = iConnectivityManagerField.get(conman);
            final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
            final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
            setMobileDataEnabledMethod.setAccessible(true);
            setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
Also Need Permission in Manifests
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

How to check Internet Connection in Android for wifi or Mobile Data connectivity?

You can Use this to check Internet Connection in Android for wifi Or Mobile Data connectivity
 public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean NisConnected = activeNetwork != null && activeNetwork.isConnected();
        if (NisConnected) {
            //  if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
           return true;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return true;
            else
                return false;
        }
        return false;
    }

Pass a String from one Activity to another Activity in Android

Post Value from Activity Class
 Intent ii = new Intent(this, GameStartPage.class);
                   // ii.putExtra("pkgName", B2MAppsPKGName);
                    ii.putExtra("pkgName", YourValue);
                    ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(ii);
Get Value from Another Activity Class
  pkgn = getIntent().getExtras().getString("pkgName");

Find application running in foreground from service in Android

Use this code in Service to get foreground apps and take your own task.
 try {
            ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

            //   final String packageName = context.getPackageName();
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                //   if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals()) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Log.i("Foreground App", appProcess.processName);
                    String B2MAppsPKGName = appProcess.processName;

                    ////////Select Package name From SQLite DataBase//////////

                    OurStoreAppsPackages = new ArrayList<String>();
                    OurStoreAppsPackages = databaseHelper.getOurAllAppsPackage();

                    if (OurStoreAppsPackages.contains(B2MAppsPKGName)) {
                        //  if (storedPackageName == true) {
                        Log.i("DatabaseForeground App", B2MAppsPKGName);

                        ////// Go to GameStartPage page ./////////
                        Intent ii = new Intent(this, GameStartPage.class);
                        ii.putExtra("pkgName", B2MAppsPKGName);
                        ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(ii);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }