Friday, 6 February 2015

Android Broadcast Receiver

Hey, in this tutorial we will learn to implement Broadcast Receiver in your Android app. Broadcast Receivers are implemented to respond to the messages broadcasted by the system or by other applications.

First create a button which will trigger a broadcast from our application.

<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"
    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="com.codingredefined.broadcastreceiver.MainActivity" >

    <Button
        android:id="@+id/btnStartService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Broadcast Intent"
        android:onClick="broadcastIntent"
        />

</RelativeLayout>



After creating the button make changes in the MainActivity.java file.

package com.codingredefined.broadcastreceiver;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    //broadcast a custom intent
    public void broadcastIntent(View view)
    {
    Intent intent= new Intent();
    intent.setAction("com.codingredefined.CUSTOM_INTENT");
    sendBroadcast(intent);
    }
}



Following the changes done in the MainActivity file, then we need to create a new file MyReceiver.java which will respond to the broadcast messages.

package com.codingredefined.broadcastreceiver;

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

public class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Broadcast Received", Toast.LENGTH_LONG).show();
}

}


Now finally we need to register our broadcast receiver in the manifest file.

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
        <receiver android:name="MyReceiver">
            <intent-filter>
                <action android:name="com.codingredefined.CUSTOM_INTENT"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>



Now the application is ready to run.


Tuesday, 3 February 2015

Android Background Service

Hey, in this tutorial I will demonstrate how to enable your application to continue working in the background without requiring any user interaction.

To start with, first of all create 2 buttons to start and stop the background service.

<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"
    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="com.codingreedefined.backgroundservice.MainActivity" >

    <Button
        android:id="@+id/btnStartService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start Service"
        android:onClick="startService"
    />
    
    <Button
        android:id="@+id/btnStopService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnStartService"
        android:text="Stop Service"
        android:onClick="stopService"
    />

</RelativeLayout>


After creating the buttons write functions to start and stop the background service in MainActivity file.

package com.codingreedefined.backgroundservice;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    //Method to start the service
    public void startService(View view)
    {
    startService(new Intent(getBaseContext(), MyService.class));
    }
    
    //Method to start the service
    public void stopService(View view)
    {
    stopService(new Intent(getBaseContext(), MyService.class));
    }
    
}




Now create a new Java class named MyService and extend Service. Finally write the following code.

package com.codingreedefined.backgroundservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service 
{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//let it continue running until it is stopped
Toast.makeText(this, "Service Started!!!", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped!!!", Toast.LENGTH_SHORT).show();
}
}


Finally add <service /> tag in the manifest file and run the application.

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
        <service android:name=".MyService" />
    </application>
</manifest>


The following output shall be obtained.







Wednesday, 7 May 2014

Hide Source Code of Web Pages

<!DOCTYPE html>
<html>
<head>
<!-- Code to prevent use of Ctrl+U -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).on('keydown',function(e)
{
    var key = e.charCode || e.keyCode;
    if(key == 17 || key == 85)
        {
e.preventDefault();
}
});
</script>
</head>
<!-- Use oncontextmenu="return false" to disable right-click -->
<body oncontextmenu="return false">
<h2>Coding Redefined</h2>
</body>
</html>