Thursday, 25 February 2016

How to get date and time from mobile network

Get Time and Date From Network In Android


Java Source Code:----

package com.rajnish.gettimefromnetwork;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener
{
TextView t;
String d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t=(TextView)findViewById(R.id.textView1);
LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
long time=loc.getTime();
Date date=new Date(time);
SimpleDateFormat dateFormat=new SimpleDateFormat("dd/mm/yyyy  HH:mm:ss");
try
{
d=dateFormat.format(date);
t.setText(d);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), d, Toast.LENGTH_LONG).show();
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

}

*********************************************************************************


XML Code :-



<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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>




*********************************************************************************

Permission Info:--- 


    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

Friday, 29 January 2016

Saturday, 31 October 2015

Wednesday, 21 October 2015

Bandwidth Monitoring in Android




Source Code of Bandwidth Monitoring in Android.

ActivityManager am=(ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<String> runingAppProcess=new ArrayList<String>();
List<RunningAppProcessInfo> runningProcesses=am.getRunningAppProcesses();
         long send       = 0;
         long recived    = 0;

for (ActivityManager.RunningAppProcessInfo process: runningProcesses) 
{
//get send or received packet
recived = TrafficStats.getUidRxBytes(process.uid);
                 send = TrafficStats.getUidTxBytes(process.uid);
}

Monday, 19 October 2015

How to get Device Information such IMEI,IMSI No,Brand Name,manufacture name, Software Version , Sim Serial No



Firsr Activity Java Code:-

public class SystemInfo extends Fragment
{
TextView txtManufacture;
Context context;
public SystemInfo(Context ctx)
{
this.context=ctx;
}
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {

DeviceInfoDao deviceInfo=new DeviceInfoDao();
deviceInfo=DeviceInfoDao.getDeviceInfo(context.getApplicationContext());
        View rootView = inflater.inflate(R.layout.system_info, container, false);
        txtManufacture=(TextView)rootView.findViewById(R.id.manufacture);
     
        txtManufacture.setText(deviceInfo.manufacturer);
        return rootView;
     
    }

}




Second Class :-



package com.networkinfo.dao;

import android.content.Context;
import android.telephony.TelephonyManager;

public class DeviceInfoDao 
{

public  String manufacturer;
public  String phoneNo;
public  String brandName;
public  String deviceModelNo;
public  String ImeiNo;
public  String ImsiGsm;
public  String osversion;
public  String bootLoader;
public  String buildId;
public  String radioNo;
public  String fingerprint;
public  String display;
public  String deviceId;
public  String countryCode;
public  String softwareVersion;
public  String deviceIMSI;
public  String simSerialNo;
public  String voiceMailNumber;
public  String simType;
public  String simRoming;
public  String simState;

static TelephonyManager telephonyManager;
public static DeviceInfoDao getDeviceInfo(Context ctx)
{
DeviceInfoDao deviceInfo=new DeviceInfoDao();
telephonyManager=(TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);
deviceInfo.manufacturer=android.os.Build.MANUFACTURER;
deviceInfo.brandName=android.os.Build.BRAND;
deviceInfo.fingerprint=android.os.Build.FINGERPRINT;
deviceInfo.bootLoader=android.os.Build.BOOTLOADER;
deviceInfo.display=android.os.Build.DISPLAY;
deviceInfo.deviceId=android.os.Build.ID;
deviceInfo.ImeiNo=telephonyManager.getDeviceId();
deviceInfo.deviceIMSI=telephonyManager.getSubscriberId();

deviceInfo.countryCode=telephonyManager.getNetworkCountryIso();
deviceInfo.softwareVersion=telephonyManager.getDeviceSoftwareVersion();

deviceInfo.simSerialNo=telephonyManager.getSimSerialNumber();
deviceInfo.voiceMailNumber=telephonyManager.getVoiceMailNumber();
int phoneType=telephonyManager.getPhoneType();

switch (phoneType) 
        {
                case (TelephonyManager.PHONE_TYPE_CDMA):
                deviceInfo.simType="CDMA";
                               break;
                case (TelephonyManager.PHONE_TYPE_GSM):
                deviceInfo.simType="GSM";
                               break;
                case (TelephonyManager.PHONE_TYPE_NONE):
                deviceInfo.simType="None";
                               break;
         }
        boolean isRoaming=telephonyManager.isNetworkRoaming();
        if(isRoaming)
        {    
        deviceInfo.simRoming="Yes";
        //phoneDetails+="\nIs In Roaming:"+"YES";
        
        }
            else
            {
            deviceInfo.simRoming="No";
              // phoneDetails+="\nIs In Roaming  "+"NO";
            }
        
        int SIMState=telephonyManager.getSimState();
        switch(SIMState)
            {
                    case TelephonyManager.SIM_STATE_ABSENT :
                    deviceInfo.simState="";
                        // your code
                        break;
                case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
                deviceInfo.simState="";                       
                break;
                    case TelephonyManager.SIM_STATE_PIN_REQUIRED :
                    deviceInfo.simState="";
                    break;
                    case TelephonyManager.SIM_STATE_PUK_REQUIRED :
                    deviceInfo.simState="";
                    break;
                    case TelephonyManager.SIM_STATE_READY :
                    deviceInfo.simState="";
                    break;
                    case TelephonyManager.SIM_STATE_UNKNOWN :
                    deviceInfo.simState="";
                    break;
            
            }
return deviceInfo;
}

}


Sunday, 31 May 2015

Simple Ripple Button Tutorial

Simple Ripple And Reveal Tutorial

Let's get party started:-

I decided to choose circular buttons to make things a bit more complicated and funnier. So in general, ripple effect for regular buttons works out of the box (in Material theme) and for other touchable views can be achieved by specifying ?android:selectableItemBackground as a background:

<LinearLayout 
    android:id="@+id/someTouchableArea"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackground">

    .....
</LinearLayout>

Creating circular button

This part is not really related to our today's topic, so feel free to skip it if you feel comfortable implementing circular button with selector.
That's what you would normally do in Holo (and prior):
  • create 3 shape drawables (for every button state): normal state, pressed state, selected state
  • create selector for the button background
  • create color selector for the text color (we need to change text color to white when button is selected)
So here we go:
layout/circularbutton_layout.xml 
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="72dip"
    android:layout_height="72dip"
    android:background="@drawable/circular_button_selector"
    android:textAppearance="?android:textAppearanceLarge"
    android:textColor="@color/button_text_selector"/>
layout/main_activity.xml
<FrameLayout 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=".MainActivity">

    <LinearLayout
        android:id="@+id/buttonsContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"/>

</FrameLayout>
MainActivity.java
package rajnish.com.rippletest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Space;


public class MainActivity extends Activity implements View.OnClickListener {

    private ViewGroup buttonsContainer;
    private Button activeButton = null;
    private final int MAX_BUTTONS = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.buttonsContainer = (ViewGroup) findViewById(R.id.buttonsContainer);

        int buttonsSpacing = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
        int buttonSize = (int) getResources().getDimension(R.dimen.button_size);

        for (int i = 0; i < MAX_BUTTONS; i++) {
            Button button = (Button) getLayoutInflater().inflate(R.layout.circular_button_layout, buttonsContainer, false);
            button.setText("Test " + i);
            button.setOnClickListener(this);
            buttonsContainer.addView(button);

            //Add margin between buttons manually
            if (i != MAX_BUTTONS - 1) {
                buttonsContainer.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
            }
        }
        selectButton((Button) buttonsContainer.getChildAt(0));
    }

    private void selectButton(Button button) {
        if (activeButton != null) {
            activeButton.setSelected(false);
            activeButton = null;
        }

        activeButton = button;
        button.setSelected(true);
    }

    @Override
    public void onClick(View view) {
        selectButton((Button) view);
    }
}
color/buttontext_selector.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#FFF"/>
    <item android:color="#777"/>
</selector>
drawable/circularbutton_selector.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/circular_button_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/circular_button_pressed"/>
    <item android:drawable="@drawable/circular_button"/>

</selector>
... and 3 shape drawables for each state - tired of copy-pasting layouts - go and check them out on GitHub (see the end of the article)
And here is what we've got:
holo
Looks..... Holo :) Not Material. Let's fix it.

Speaking Material

First thing we need to do - is to create new ripple selector which will contain our old (slightly modified) holo selector:
drawabale/ripple_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask" android:drawable="@drawable/circular_button"/>
    <item android:drawable="@drawable/circular_button_selector"/>
</ripple>
Allright, what we have here. The new ripple xml tag describes what our ripple should look like.
  • android:color - defines the color of ripple. I changed ?android:colorControlHighlight attribute in my theme to match my main accent color. In this way built-in ripple effects for default buttons will get the same color.
  • first item with id @android:id/mask defines the shape of my ripple. If we do not specify mask - shape will be determined by the shape of all other leyers. So in my case specifying mask was not really necessary.
  • second item is my old selector resource. I still want my button to have it's selected state, so I have to specify it here. The only modification I need to do in my old selector - is to get rid of pressed state. I don't need it because during "press" action - my ripple will kick in.
  • if you specify empty "ripple" tag (with no children) - you will get borderless circular ripple effect
drawable/circularbutton_selector
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/circular_button_selected"/>
    <item android:drawable="@drawable/circular_button"/>
</selector>
Now let's replace our button's background attribute with our new ripple selector and see what happens:
ripple
Looks nicer already! But still kinda plain.

Elevation

Really neat trick for making your views feel Material - is giving them a bit of elevation in response to touch. In Android L you can easily do this withandroid:stateListAnimator. It looks like a regular selector, but instead of state drawables you can specify custom objectAnimator which will start an animation when view changes it's state:
anim/button_elevation.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueFrom="2dip"
            android:valueTo="4dip"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueFrom="4dip"
            android:valueTo="2dip"
            android:valueType="floatType" />
    </item>
</selector>
So here we asked stateListAnimator to animate view elevation from "2" to "4" when button is pressed and back when released. Now we need to set thisstateListAnimator to our button:
layout/circularbutton_layout
<?xml version="1.0" encoding="utf-8"?>

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/button_size"
    android:layout_height="@dimen/button_size"
    android:background="@drawable/circular_button_ripple_selector"
    android:textAppearance="?android:textAppearanceLarge"
    android:textColor="@color/button_text_selector"
    android:stateListAnimator="@anim/button_elevation"/>
Note: there is a way to specify stateListAnimator via code (View#setStateListAnimator()), but I couldn't find a way to get StateListAnimator object from resources (like we did with regular animations). If anybody knows how to do that - please let me know
If we run this code - we will get this ugly result:
Invalid shadow
The problem is that shadow doesn't know anything about our custom button shape, so by default it thinks it is rectangular. To fix this - we need to specify Outline to our button:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...........
    int buttonSize = (int) getResources().getDimension(R.dimen.button_size);
    Outline circularOutline = new Outline();
    circularOutline.setOval(0, 0, buttonSize, buttonSize);

    for (int i = 0; i < MAX_BUTTONS; i++) {
        Button button = ......
        .......
        button.setOutline(circularOutline);
        ........
    }
    .....
}
Now we got really nice looking shadows below our buttons + neat elevation touch feedback in addition to our ripple:
elevation shadow

Reveal

Even though we got really nice looking Material-themed widget, I would like to go one step further and try to implement something similar to the reveal effect described by +RomanNurik here. What I want to do - is to make my button reveal it's "selected" state instead of simply changing it's color.
Want to point out that the way I implemented it might not be the correct way of doing such kind of things since it looks messy to me. Anyways, this is the best I have so far, so let's wait until Google I/O app source code is released, so we can take a peak at how it needs to be done.
So what is "reveal"? It is just an animation which animates view's clipping boundaries. Android provides really convinient helper method to create this animation:ViewAnimationUtils#createCircularReveal(View view, int centerX, int centerY, float startRadius, float endRadius). As we can see, everything is pretty straight forward:
  • view - view to reveal
  • centerX - start X coordinate of reveal
  • centerY - start Y coordinate of reveal
  • startRadius - start radius. In most cases - 0
  • endRadius - end radius - depends on your view's bounds
So in theory, once item becomes selected - we need to create reveal animation and start it:
MainActivity.java
private void selectButton(Button button) {
    if (activeButton != null) {
        activeButton.setSelected(false);
        activeButton = null;
    }

    activeButton = button;
    button.setSelected(true);

    ViewAnimationUtils.createCircularReveal(button,
            button.getWidth(),
            button.getHeight(),
            0,
            button.getHeight() * 2).start();
}
invalid reveal
Em... Looks... Weird. The stroke part in our shape gets revealed as well. What I really want - is to leave stroke part always the same and reveal color part only.
I couldn't find a proper way of revealing only a part my view, so I decided to wrap my button into a separate layout which has stroke border as a background. In this case when I reveal my button - background part stays the same, so it looks like only color part is revealed.
I realize, this is not the best way since I created additional overdraw and added another level to my view hierarchy. But since reveal - is just an animation - we can optimize it by displaying this additional level only when animation is in progress. Hope you can figure that out yourself.
circularbutton_layout.xml
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/button_size"
    android:layout_height="@dimen/button_size"
    android:background="@drawable/circular_button">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/circular_button_ripple_selector"
        android:textAppearance="?android:textAppearanceLarge"
        android:textColor="@color/button_text_selector"
        android:stateListAnimator="@anim/button_elevation"/>

</FrameLayout>
And here is what it looks like. I intentionally slowed down animation to see what actually happens:
correct reveal

The last nitpick. I promise :)

Right now no matter where we touch our button, reveal goes from bottom-right corner and goes up to the left corner. It would feel more natural if reveal starts right where you released your finger (just like RippleDrawable does).
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    ........
    for (int i = 0; i < MAX_BUTTONS; i++) {
        ........
        button.setOnTouchListener(this);
        .........
    }
    ......
}

private void selectButton(Button button, boolean reveal, int startX, int startY) {
    ........
    ViewAnimationUtils.createCircularReveal(activeButton,
                startX,
                startY,
                0,
                activeButton.getHeight()).start();
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        selectButton((Button) view, true, (int) motionEvent.getX(), (int) motionEvent.getY());
    }
    return false;
}
And here is what it looks like:
Final result