Monday, 24 October 2016

Get Device Information In Android

DeviceInfoActivity.java

public class DeviceInfoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        String deviceId = Settings.System.getString(getContentResolver(),
                Settings.System.ANDROID_ID);
       
        TextView tv1 = (TextView) findViewById(R.id.textView1);
       
        String MANUFACTURER=android.os.Build.MANUFACTURER;
        String kernelversion=System.getProperty("os.version");
        Button b = (Button)findViewById(R.id.button1);
       

       
        TextView device = (TextView)findViewById(R.id.device);
        TextView model = (TextView)findViewById(R.id.model);
        TextView product = (TextView)findViewById(R.id.product);
        TextView codename = (TextView)findViewById(R.id.codename);
        TextView incremental = (TextView)findViewById(R.id.incremental);
        TextView release = (TextView)findViewById(R.id.release);
        TextView sdk = (TextView)findViewById(R.id.sdk);
        TextView sdkInt = (TextView)findViewById(R.id.sdk_int);
        TextView kernel_version=(TextView)findViewById(R.id.kernel_version);
        TextView Manufacture=(TextView)findViewById(R.id.Manufacture);
        TextView BuildNumber =(TextView)findViewById(R.id.buildnumber);
       

        tv1.setText("device Id "+deviceId);
        kernel_version.setText("Kernel Vrsion"+kernelversion);
        Manufacture.setText("Manufacture:: "+MANUFACTURER);
        BuildNumber.setText("Build Number :: "+android.os.Build.HOST);
        device.setText("android.os.Build.DEVICE: " + android.os.Build.DEVICE);
        model.setText("android.os.Build.MODEL: " + android.os.Build.MODEL);
        product.setText("android.os.Build.PRODUCT: " + android.os.Build.PRODUCT);
        codename.setText("android.os.Build.VERSION.CODENAME: " + android.os.Build.VERSION.CODENAME);
        incremental.setText("android.os.Build.VERSION.INCREMENTAL: " + android.os.Build.VERSION.INCREMENTAL);
        release.setText("Android Version : " + android.os.Build.VERSION.RELEASE);
        sdk.setText("android.os.Build.VERSION.SDK: " + android.os.Build.VERSION.SDK);
        sdkInt.setText("android.os.Build.VERSION.SDK_INT: " + String.valueOf(android.os.Build.VERSION.SDK_INT));
 
       
        Account[] accounts = AccountManager.get(this).getAccounts();
        String possibleEmail =null;
       
        AccountManager a=AccountManager.get(getApplicationContext());
        for (Account account : accounts) {
          // TODO: Check possibleEmail against an email regex or treat
          // account.name as an email address only for certain account.type values.
        possibleEmail =possibleEmail+"-"+ account.name;
       
        }
       
        BuildNumber.setText("Build Number :: "+possibleEmail);
       
        b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
});
        //Toast.makeText(getApplicationContext(), "Hii"+deviceId,10000).show();
       
    }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
   
   <TextView
        android:id="@+id/device"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
   
<TextView
        android:id="@+id/model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
<TextView
        android:id="@+id/product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
<TextView
        android:id="@+id/codename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
<TextView
        android:id="@+id/incremental"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
<TextView
        android:id="@+id/release"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />  

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


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

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

<TextView
        android:id="@+id/buildnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidhive"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name="DeviceInfoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
     
    </application>

</manifest>

Output

Android custom circle progress bar

MyActivity.java

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animations();
    }
    
public void animations(){
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int loop=0;loop<35;loop++){
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar);
    progressbar.setProgress(loop);
    final long timeinterval = 100;
    try {
Thread.sleep(timeinterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
});

}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.myApp.jiohome.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:indeterminate="false"
        android:layout_centerInParent="true"
        android:progressDrawable="@layout/circle_progress_bar"
        android:background="@layout/circle_shape"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
         
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:text="hello_world"
        android:id="@+id/balanceText"></TextView>
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"></TextView>
   
   </LinearLayout>
   
</RelativeLayout>

circle_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadiusRatio="2.5"
        android:shape="ring"
        android:thickness="8dp"
        android:useLevel="true"
        android:background="#ff00ff"><!-- this line fixes the issue for lollipop api 21 -->

        <gradient
            android:angle="0"
            android:endColor="#ff00ff"
            android:startColor="#ff00ff"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
 >
<corners android:radius="20dip"/>
<stroke android:color="@android:color/transparent" android:width="50dip"/>
<solid android:color="#efefef"/>
</shape>

circular.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="6"
            android:shape="ring"
            android:thicknessRatio="20.0"
            android:useLevel="true">


            <gradient
                android:centerColor="#999999"
                android:endColor="#999999"
                android:startColor="#999999"
                android:type="sweep" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">

            <shape
                android:innerRadiusRatio="6"
                android:shape="ring"
                android:thicknessRatio="20.0"
                android:useLevel="true">


                <rotate
                    android:fromDegrees="0"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:toDegrees="360" />

                <gradient
                    android:centerColor="#00FF00"
                    android:endColor="#00FF00"
                    android:startColor="#00FF00"
                    android:type="sweep" />

            </shape>
        </rotate>
    </item>
</layer-list>

Output

AES Encryption in Android



MainActivity.java 

package com.example.encryptionexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    String seedValue = "This Is MySecure";
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
              String normalText = "RAJESH";
           String normalTextEnc;
                  try {
                 AESCrypt a=new AESCrypt(seedValue);
                          TextView txe = new TextView(this);
                          txe.setTextSize(14);
                          txe.setText("Normal Text ::"+normalText +" \n Encrypted Value :: "+ a.encrypt(normalText) +" \n Decrypted value :: "+a.decrypt(a.encrypt(normalText)));
                          setContentView(txe);
                  } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
       

       
    }
}

AESCrypt.java

package com.example.encryptionexample;

import java.security.MessageDigest;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class AESCrypt {

private final Cipher cipher;
private final SecretKeySpec key;
private AlgorithmParameterSpec spec;


public AESCrypt(String password) throws Exception
{
   // hash password with SHA-256 and crop the output to 128-bit for key
   MessageDigest digest = MessageDigest.getInstance("SHA-256");
   digest.update(password.getBytes("UTF-8"));
   byte[] keyBytes = new byte[32];
   System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

   cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
   key = new SecretKeySpec(keyBytes, "AES");
   spec = getIV();
}       

public AlgorithmParameterSpec getIV()
{
   byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
   IvParameterSpec ivParameterSpec;
   ivParameterSpec = new IvParameterSpec(iv);

   return ivParameterSpec;
}

public String encrypt(String plainText) throws Exception
{
   cipher.init(Cipher.ENCRYPT_MODE, key, spec);
   byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
   String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");

   return encryptedText;
}

public String decrypt(String cryptedText) throws Exception
{
   cipher.init(Cipher.DECRYPT_MODE, key, spec);
   byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
   byte[] decrypted = cipher.doFinal(bytes);
   String decryptedText = new String(decrypted, "UTF-8");

   return decryptedText;
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.encryptionexample.MainActivity">
  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
  </RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.encryptionexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



Step to creating new application in Eclipse

Create Android Application

The first step is to create a simple Android Application using Eclipse IDE. 
Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list. 







Android project directories and files 

src
This contains the .java source files for your project. By default, it includes a MainActivity.java source file having an activity class that runs when your app is launched using the app icon.

gen
This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.

bin
This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.

res/drawable-hdpi
res/drawable-xhdpi
res/drawable-xxhdpi
res/drawable-xxxhdpi
This is a directory for drawable objects that are designed for density wise screens.

res/layout
This is a directory for files that define your app's user interface.

res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colours definitions.

AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.

MainActivity.jav
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApplication</string>
    <string name="hello_world">My First Application</string>
    <string name="action_settings">Settings</string>
</resources>

Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Output