Saturday 27 July 2013

Youtube Player for playing videos

Now you have YouTube player ,so no need to use Web view or any other player.
add below code in your main.xml file where you want YouTube player.

   <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtube_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />



Now ,Create one class name
DeveloperKey 
 
public class DeveloperKey {

// This is developer key you can get it from creating project in Google API Console.
  public static final String DEVELOPER_KEY = "This is your developer key";
 
}


Create second class name YouTubeFailureRecoveryActivity

public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

  private static final int RECOVERY_DIALOG_REQUEST = 1;

  @Override
  public void onInitializationFailure(YouTubePlayer.Provider provider,
      YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
      errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
      String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
      Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOVERY_DIALOG_REQUEST) {
      // Retry initialization if user performed a recovery action
      getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
    }
  }

  protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();

}

This is your main activity

public class MainActivity extends YouTubeFailureRecoveryActivity {

       YouTubePlayerView youTubeView;
       private YouTubePlayer youTubePlayer;
   
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        youTubeView = (YouTubePlayerView)findViewById(R.id.youtube_view);
        youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }
     @Override
      public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
          boolean wasRestored) {
             if (!wasRestored) {
             // wKJ9KzGQq0w is your video id
                  player.cueVideo("wKJ9KzGQq0w");
                }   
      }
      @Override
      protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
      }
}



Don't forget to give Internet permission in your manifest file.
also you need to get YouTubeAndroidPlayerApi.jar

Friday 26 April 2013

Display Android Google Map V2

==> Create New Application.
==> Install Google Play Serivces Library in to your application.
==> To Get Google Play Services you need to update your SDK with google play  services.
==> After that you need to add "google play services" library project in to your workspace ,after add into your application.
==> In your main.xml file write below code. 

  <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="300dp"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>


==> In your Main.Java Activity ,extends FragmentActivity ,define as below. 

public class MainActivity extends FragmentActivity {

    SupportMapFragment mMapFragment;
    GoogleMap mMap;
   
@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.main);
    mMap = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
   
}


==> In your Manifest.xml file define some permissions and code for Map V2 as below.

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

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

     <permission
        android:name="com.example.mapv2demo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="com.example.mapv2demo.permission.MAPS_RECEIVE" />
     <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
   
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mapv2demo.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>
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Your API Key" />
    </application>

</manifest>
 
 
==> And Now run this application , it Display Google Map V2.  
 
   

Get Android Google Map Release Key

  1. Make One Folder in any drive. (i.e. Androidmapkey) 
  2. Copy your application keystore key , and Paste it in new created folder.
  3. Open Command Prompt.
  4. Go to your path "C:\Program Files (x86)\Java\jdk1.6.0_02\bin"
    After that write "keytool -v -list -keystore C:\"Your created folder" (i.e. AndroidMapKey)\your application keystore(i.e. demo.keystore)"  and press enter.
  5. In password give any password.
  6. It Create's Like defines as below.

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: androiddebugkey
Creation date: Feb 20, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 5124695f
Valid from: Wed Feb 20 11:42:47 IST 2013 until: Fri Feb 13 11:42:47 IST 2043
Certificate fingerprints:
         MD5:  00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
         SHA1: 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
         Signature algorithm name: SHA1withRSA
         Version: 3


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

 Using this MD5 and SHA1 Key Certificate you can take API Key.

Get Android Google Map Debug API Key

  1. Make One Folder in any drive. (i.e. Androidmapkey) 
  2. Copy your debug key from "C" Drive "Users" folder , and Paste it in new created folder.
  3. Open Command Prompt.
  4. Go to your path "C:\Program Files (x86)\Java\jdk1.6.0_02\bin"
    After that write "keytool -v -list -keystore C:\"Your created folder" (i.e. AndroidMapKey)\debug.keystore"  and press enter.
  5. In password give any password.
  6. It Create's Like defines as below.
 
*****************  WARNING WARNING WARNING  *****************
* The integrity of the information stored in your keystore  *
* has NOT been verified!  In order to verify its integrity, *
* you must provide your keystore password.                  *
*****************  WARNING WARNING WARNING  *****************

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: androiddebugkey
Creation date: Feb 20, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 5124695f
Valid from: Wed Feb 20 11:42:47 IST 2013 until: Fri Feb 13 11:42:47 IST 2043
Certificate fingerprints:
         MD5:  00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
         SHA1: 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
         Signature algorithm name: SHA1withRSA
         Version: 3


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

 Using this MD5 and SHA1 Key Certificate you can take API Key.

Monday 29 October 2012

Get Device Angle


public class Get_Angle extends Activity  implements SensorEventListener{

public static final String tag = Get_Angle.class.getSimpleName();
public static Integer x;
public static Integer y;
private SensorManager sensorManager = null;
TextView txtanglex,txtangley;
Bitmap oldBitmap ;
String filePath = null;
float degrees;
ImageView img;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

setContentView(R.layout.main);

bindComponent();
addLisners();
}
private void addLisners() {
// TODO Auto-generated method stub

}

private void bindComponent() {
// TODO Auto-generated method stub
txtanglex = (TextView)findViewById(R.id.textxpos);
txtangley = (TextView)findViewById(R.id.textypos);
img = (ImageView)findViewById(R.id.imageView1);
}
private void init() {

// TODO Auto-generated method stub
oldBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arleft);
int w = oldBitmap.getWidth();
int h = oldBitmap.getHeight();

Matrix mtx = new Matrix();
degrees = Integer.parseInt(x.toString());
mtx.postRotate(-x);

oldBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, w, h, mtx, true);
img.setImageBitmap(oldBitmap);

}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

x = (int) Math.pow(event.values[0], 2);
y = (int) Math.pow(event.values[1], 2);

txtanglex.setText(x.toString());
txtangley.setText(y.toString());
Log.i("x is","x is.."+x.toString());

}
init();

   }
}

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...