19 Jul 2012

Alarm Service | AlarmManagerExample in android

Here we are setting the time to 10 seconds so after 10 seconds a Toast pops up.
For receiving the Broadcast we have to create a class the extends the BroadcastReceiver class on which the onReceive function gets called when the service ends

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="Alarm Demo"/>

    <Button android:id="@+id/one_shot"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Start Alarm">
        <requestFocus />
    </Button>
</LinearLayout>

AndroidManifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hb"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".AlarmDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


MyAlarmReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyAlarmReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
Toast.makeText(context, "Alarm Received after 10 seconds.", Toast.LENGTH_SHORT).show();
    }
}

AlarmDemo.class
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlarmDemo extends Activity {
       Toast mToast;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);

              Button button = (Button)findViewById(R.id.one_shot);
              button.setOnClickListener(mOneShotListener);

       }
       private OnClickListener mOneShotListener = new OnClickListener() {
              public void onClick(View v) {

                     Intent intent = new Intent(AlarmDemo.this,MyAlarmReceiver.class);
                     PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
                                  0, intent, 0);

                     // We want the alarm to go off 10 seconds from now.
                     Calendar calendar = Calendar.getInstance();
                     calendar.setTimeInMillis(System.currentTimeMillis());
                     calendar.add(Calendar.SECOND, 10);

                     // Schedule the alarm!
                     AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                     am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

                     // Tell the user about what we did.
                     if (mToast != null) {
                           mToast.cancel();
                     }
                     mToast = Toast.makeText(AlarmDemo.this,"Alarm Started",
                                  Toast.LENGTH_LONG);
                     mToast.show();
              }
       };
}

Ref Link:
Link1:
Link2:
AlarmManagerExample : Visit 

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)


Open wifi settings in android?

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="How to open Wifi Settings Demo" />

    <Button
        android:id="@+id/open"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Wifi settings" />

</LinearLayout>



OpenWifiSettingsDemo.class

public class OpenWifiSettingsDemo extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button open = (Button)findViewById(R.id.open);
            open.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                        openWifiSettings();
                  }
            });
      }

      public void openWifiSettings(){

            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity( intent);
      }
}


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

18 Jul 2012

PullToRefresh in android

Hello Friends, here today i am going to post Pull To refresh sample, It allows a user to refresh a list by pulling down and releasing from the top of the list. Hop this will help you.


Download Source code from here: PullToRefreshSampleCode

PullToRedresh With Load More : SampleCode

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)

17 Jul 2012

View-flipper in Android.

In this sample code contain sweeping images using View flipper with animation xml.




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="@string/hello" />

    <ViewFlipper
        android:id="@+id/vfShow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ViewFlipper>

</LinearLayout>


Create xml file in drawable folder
1.left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />

</set>


2.left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

3.right_in.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />

</set>


4.right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>


ViewFlipperSampleActivity.class


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class ViewFlipperSampleActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

private ViewFlipper vf;
private Context mContext;
private final GestureDetector detector = new GestureDetector(new MyGestureDetector());

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(final View view, final MotionEvent event)
{
detector.onTouchEvent(event);
return true;
}
});

vf.addView(addImageView(R.drawable.scott));
vf.addView(addImageView(R.drawable.ricardo));

}

View addImageView(int resId)
{
ImageView iv = new ImageView(this);
iv.setImageResource(resId);

return iv;
}

class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
try
{

// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
vf.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.left_out));
vf.showNext();
return true;
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
vf.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_out));
vf.showPrevious();
return true;
}

}
catch (Exception e)
{
e.printStackTrace();
}

return false;
}
}
}

Download sample code from here : ViewFlipperSampleCode

Dynamic Swipe view using gesture:

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)