Friday, May 13, 2016

AJAX

What is AJAX?

AJAX = Asynchronous JavaScript and XML.

AJAX is based on internet standards, and uses a combination of:
  • XMLHttpRequest object (to retrieve data from a web server)
  • JavaScript/DOM (to display/use the data)

The keystone of AJAX is the XMLHttpRequest object.

The XMLHttpRequest Object

All modern browsers support the XMLHttpRequest object.
The XMLHttpRequest object is used to exchange data with a server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

Create an XMLHttpRequest Object

All modern browsers (Chrome, IE7+, Firefox, Safari, and Opera) have a built-in XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
variable new XMLHttpRequest();

Thursday, May 12, 2016

wallpaperSWAP

Create a wallpaperSWAP in android
WRITE A CODE IN MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Change"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="match_parent">
</TextView>

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio0" android:text="@string/onem"
android:checked="true"
android:layout_width="match_parent">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio1"
android:text="@string/five"
android:layout_width="match_parent">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio2"
android:text="@string/ten"
android:layout_width="match_parent">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio3"
android:text="@string/thirty"
android:layout_width="match_parent">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:id="@+id/radio4"
android:text="@string/oneh"
android:layout_width="match_parent">
</RadioButton>

</RadioGroup>

<Button
android:layout_height="wrap_content"
android:text="@string/Set" android:id="@+id/button1"
android:layout_width="match_parent"
android:textSize="25sp">
</Button>

<Button
android:layout_height="wrap_content"
android:text="@string/Unset" android:id="@+id/button2"
android:layout_width="match_parent"
android:textSize="25sp">
</Button>

</LinearLayout>

WALLPAPERMAIN.java
package com.example.wallswap;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.Intent;

import android.os.Bundle;

public class WallSwapActivity extends Activity {
    /** Called when the activity is first created. */
private Button mSetButton;
private Button mUnsetButton;
private RadioButton mOneMinRadio;
private RadioButton mFiveMinRadio;
private RadioButton mTenMinRadio;
private RadioButton mThirtyMinRadio;
private RadioButton mOneHourRadio;
private RadioGroup mTimeRadioGroup;
public int mChangeTime = 60;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSetButton = (Button)findViewById(R.id.button1);
        mUnsetButton = (Button)findViewById(R.id.button2);
        mOneMinRadio = (RadioButton)findViewById(R.id.radio0);
        mFiveMinRadio = (RadioButton)findViewById(R.id.radio1);
        mTenMinRadio = (RadioButton)findViewById(R.id.radio2);
        mThirtyMinRadio = (RadioButton)findViewById(R.id.radio3);
        mOneHourRadio = (RadioButton)findViewById(R.id.radio4);
        mTimeRadioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
        mUnsetButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent mDisableIntent = new Intent(WallSwapActivity.this, WallpaperChangeService.class);
            stopService(mDisableIntent);
            finish();
        }
        });

        mSetButton.setOnClickListener(new OnClickListener() {

        @Override

        /* Set the value of mChangeTime variable according to the radio button selected by the user */

        public void onClick(View arg0) {
        int mRadioID = mTimeRadioGroup.getCheckedRadioButtonId();
        if(mOneMinRadio.getId()==mRadioID){mChangeTime=60;}
        else if(mFiveMinRadio.getId()==mRadioID){mChangeTime=5*60;}
        else if(mTenMinRadio.getId()==mRadioID){mChangeTime=10*60;}
        else if(mThirtyMinRadio.getId()==mRadioID){mChangeTime=30*60;}
        else if(mOneHourRadio.getId()==mRadioID){mChangeTime=60*60;}

        /* Create an intent destined for the service */
        Intent mServiceIntent = new Intent(WallSwapActivity.this, WallpaperChangeService.class);

        /* Create a bundle and store in it a key-value pair specifying the time after which the wallpaper needs to be changed. */

        Bundle mBundleTime = new Bundle();
        mBundleTime.putInt("time", mChangeTime);

        /* Attach the bundle to the intent */

        mServiceIntent.putExtras(mBundleTime);

        /* Start the service */
        startService(mServiceIntent);

        /* End the activity */
        finish();
        }
        });
    }
}

WALLPAERCHANGESERVICES.JAVA
package com.example.wallswap;

import android.app.Service;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.content.Intent;
import android.os.IBinder;

public class WallpaperChangeService extends Service implements Runnable {

/* Store references to the wallpaper images in an array */
private int wallpaperId[] = {R.drawable.wallpaper1,R.drawable.wallpaper2};
/* Declare a variable to store the time selected by the user */
private int time;

/* Declare a flag to check which image to display next */
private int FLAG=0;

private Thread t;

/* Declare two bitmap objects to store the wallpaper images */
private Bitmap bitmap;
private Bitmap bitmap1;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
/* The overridden onStartCommand() method */
@Override
public int onStartCommand(Intent intent, int flag,int startId)
{
super.onStartCommand(intent,flag, startId);
/* Obtain the bundle sent along with the intent sent from the activity */
Bundle bundle=intent.getExtras();
/* Obtain the value of the time key */
time = bundle.getInt("time");

/* Initialize the Bitmap objects with the wallpaper images */
bitmap=BitmapFactory.decodeResource(getResources(),wallpaperId[0]);
bitmap1=BitmapFactory.decodeResource(getResources(),wallpaperId[1]);

/* Start a new thread to keep the service running in the background. */
t = new Thread(WallpaperChangeService.this);
t.start();
return 0;
}

/* The overridden onDestroy() method */

@Override
public void onDestroy(){
super.onDestroy();
System.exit(0);
}

/* The run() method containing code that is executed by the newly created 
thread */
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(true){
if(FLAG==0) {
this.setWallpaper(bitmap);
FLAG++;
}
else{
this.setWallpaper(bitmap1);
FLAG--;
}
Thread.sleep(1000*time);
}
} catch (Exception e){
e.printStackTrace();
}
}

}