Tuesday, August 19, 2014

How to Display an Alert Dialog in a Fragment

 Display an Alert Dialog in a Fragment

//SplashScreenActivity.class is your Launcher Activity
 // In Case of Activity instead of fragment Replace getActivity() with getApplicationContext()

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setCancelable(false);
            builder.setTitle(Html.fromHtml("<font color='#7F02AE'><b>Star Cineplex</b></font>"));
            builder.setMessage(Html.fromHtml("<font color='#120049'>Your data services are not working.Please check your data services.</font>"));
            builder.setPositiveButton(Html.fromHtml("<font color='#7F02AE'><b>OK</b></font>"), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                                         
  Intent intent = new Intent(getActivity(), SplashScreenActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("EXIT", true);
                        startActivity(intent);
            
                                  
                   
                }
            });
                   AlertDialog alert=builder.create();
            alert.show();

Now at OnCreate of the Launcher Activity(eg. SplashScreenActivity.class)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_image_mn);
        //To exit
        if (getIntent().getBooleanExtra("EXIT", false)) {
            SplashScreenActivity.this.finish();
            System.exit(0);
        }
Like the following:
                     
            

Wednesday, August 13, 2014

How to get IMEI or ESN Programmatically in Android

To Get IMEI or ESN Programmatically in Android


package mn.androidms_imei;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        TextView textDeviceID = (TextView)findViewById(R.id.deviceid);

        //retrieve a reference to an instance of TelephonyManager
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        textDeviceID.setText(getDeviceID(telephonyManager));

    }

    String getDeviceID(TelephonyManager phonyManager){

     String id = phonyManager.getDeviceId();
     if (id == null){
      id = "not available";
     }

     int phoneType = phonyManager.getPhoneType();
     switch(phoneType){
     case TelephonyManager.PHONE_TYPE_NONE:
      return "NONE: " + id;

     case TelephonyManager.PHONE_TYPE_GSM:
      return "GSM: IMEI=" + id;

     case TelephonyManager.PHONE_TYPE_CDMA:
      return "CDMA: MEID/ESN=" + id;

     /*
      *  for API Level 11 or above
      *  case TelephonyManager.PHONE_TYPE_SIP:
      *   return "SIP";
      */


     default:
      return "UNKNOWN: ID=" + id;
     }

    }
   
}

//***activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/deviceid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

//***Add Permission 

android.permission.READ_PHONE_STATE

Tuesday, August 12, 2014

Display Alert Dialog on Back button Pressed in Android

How to Exit With an Alert Dialog Using back button in Android


    @Override
    public void onBackPressed() {
              
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setMessage("Do you want to Exit?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //if user select "Yes", app will exit
              Intent intent = new Intent(Intent.ACTION_MAIN);
              intent.addCategory(Intent.CATEGORY_HOME);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(intent);
              finish();
              System.exit(0);


               
               
            }
        });
        builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //if user select "No", just cancel this dialog and continue with app
                dialog.cancel();
            }
        });
        AlertDialog alert=builder.create();
        alert.show();
    }