Cdacians

Cdacians
Cdacians

Friday 14 September 2012

Android screen crack prank


Android screen crack prank
In this post, we are going to develop an application, its kinda game which can be used to prank your friends. :) . Yes, you want to fool your friends with a simple android application, just go through the post to develop one for you :) . I would like to name this application – ‘Screen crack prank’.
Application’s theme:
Let your friends play the game on your Android phone and see how the screen cracks and the screen goes black and also vibrates. Your friends will believe they broke your phone.
You must have already seen such applications in Google play but creating one such application is really fun!!
Let us start creating the application:
Either you can proceed with the below listings, or you can directly download code.
New Visitor? Like what you read? Then do subscribe to 
  • Create new android project [File >> New >> Android Project] with project name TapperApp
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish
Layout creation:
We will create a simple layout which has one textview to display instructions and one button who is the hero of ‘Screen Crack Prank’ app. :)
Main.xml:
Replace the XML present in /res/layout with the below one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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"
    android:id="@+id/background">
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Challenge for you!"
        android:textStyle="bold"
        android:textColor="#0587d9"
        android:textSize="20sp"
        android:layout_margin="20dip"
        android:gravity="center"
        android:id="@+id/title"/>
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:layout_margin="20dip"
        android:text="INSTRUCTIONS:\n\nTry to tap the button 100 times within 30 seconds.\n\nBest of Luck!"
        android:id="@+id/blurb"/>
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:padding="25dip"
        android:text="100"
        android:layout_gravity="center"
        android:id="@+id/button"/>
 
</LinearLayout>
The application layout should look like:

We have to add few resources before we proceed with writing logic for the application.
Load cracked_screen.png (crack image) under /res/drawable-hdpi folder and glass.ogg (glass breaking sound file) under /res/raw folder. If raw folder is not created, create one under /res folder.
Application logic:
TapperAppActivity.java:
Logic behind the application is given below:
Open TapperAppAcitivty class under com.prgguru.android package and add the below class. Each line of code is commented to let you understand the code logic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.prgguru.android;
 
import java.util.Random;
import com.example.tapperapp.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class TapperAppActivity extends Activity implements OnClickListener{
    MediaPlayer mPlayer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set application layout
        setContentView(R.layout.main);
        //Assign media player object with sound file
        mPlayer = MediaPlayer.create(TapperAppActivity.this, R.raw.glass);
        //Find the button           
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
    }
 
    public void onClick(View v) {
        //If our applicaitons' button is clicked
        if (v.getId()==R.id.button) {
            //Get the button text which is 100
            int i = Integer.parseInt(((Button) v).getText().toString());
            //Decrement button text value and set it as button text
            ((Button) v).setText(Integer.toString(--i));
            //Create Random object
            Random gen = new Random();
            //If random value btwn (0 - 9)+1 equals to 10 or i is lesser than 50
            if ((gen.nextInt(10) + 1 == 10) || (i<50)) {
                //Call crack method
                crack();
                //Make listener to button as null
                ((Button) v).setOnClickListener(null);
            }
        }
    }
 
    private void crack() {
        //Call audio effects method
        audibleFX();
        //Call visual effects method
        visualFX();
        //Call vibrate effects method
        touchFX();
    }
 
    private void audibleFX() {
        //Play sound file
        mPlayer.start();
    }
 
    private void visualFX() {
        //Set background with broken glass image
        findViewById(R.id.background).setBackgroundResource(R.drawable.cracked_screen);
        //Set title textview with color 0x6400ff00
        ((TextView)findViewById(R.id.title)).setTextColor(0x6400ff00);
        //Set blurb textview with color 0x64ffffff
        ((TextView)findViewById(R.id.blurb)).setTextColor(0x64ffffff);
        //Set button  with color 0x64cdc9c9
        ((Button)findViewById(R.id.button)).setBackgroundColor(0x64cdc9c9);
    }
 
    private void touchFX() {
        //Create vibrator object
        Vibrator mv = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        //Vibrate for 500 MS
        mv.vibrate(new long[]{ 0, 500, 0 }, -1);
    }
 
    protected void onDestroy() {
        super.onDestroy();
        // Release mediaplayer
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}
Add vibrate permission in Androidmanifest.xml:
1
<uses-permission android:name="android.permission.VIBRATE" />
To know more about how to use vibration service in Android, see Android vibrate example
Demo:
Congratulations, We are done.
Let us test the application:
Right click on the project >> Run as >> Android application >> Choose emulator or device
Tap on the button and see what happens.

Thanks
akm
www.cdacians.com

No comments:

Post a Comment