Thursday 24 December 2015

Android - Broadcast Receivers

Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action

There are following two important steps to make BroadcastReceiver works for the system broadcasted intents −
·        Creating the Broadcast Receiver.
·        Registering Broadcast Receiver
There is one additional steps in case you are going to implement your custom intents then you will have to create and broadcast those intents
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
   
    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Welcome to Broadcast Receiver"
       
android:id="@+id/textView"
       
android:layout_gravity="center_horizontal" />
</
LinearLayout>

MainActvity.Java
package com.sra.ani.broadcastreceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
    registerReceiver(
new MyBroadcast(),new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
}

MyBroadcast.Java
package com.sra.ani.broadcastreceiver;

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

/**
 * Created by Sra on 12/24/15.
 */
public class MyBroadcast extends BroadcastReceiver {
   
@Override
   
public void onReceive(Context context, Intent intent) {
       
int bstatus=intent.getIntExtra("level",0);
        Toast.makeText(context,
"Your Battery Status is : "+bstatus, Toast.LENGTH_SHORT).show();
    }
}

Output




Monday 23 November 2015

Android - Services

A service is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if application is destroyed. A service can essentially take two states −
A service has life cycle callback methods that you can implement to monitor changes in the service's state and you can perform work at the appropriate stage. The following diagram on the left shows the life cycle when the service is created with startService() and the diagram on the right shows the life cycle when the service is created with bindService(): (image courtesy : android.com )

To create an service, you create a Java class that extends the Service base class or one of its existing subclasses. The Servicebase class defines various callback methods and the most important are given below. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect.
actvity_main.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="com.android.anil.services.MainActivity"
   
tools:showIn="@layout/activity_main"
   
android:orientation="vertical">
   
    <
Button
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:text="Start Service"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="50dp" />

    <
Button
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:text="Stop Service"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="50dp" />
</
LinearLayout>

MainActvity.Java
----------------------
package com.android.anil.services;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button
startser,stopser;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
startser=(Button)findViewById(R.id.button);
       
stopser=(Button)findViewById(R.id.button2);
       
startser.setOnClickListener(new View.OnClickListener() {
            
@Override
           
public void onClick(View v) {
                Intent intent=
new Intent(getApplicationContext(),MyServices.class);
                startService(intent);
            }
        });
       
stopser.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                Intent intent=
new Intent(getApplicationContext(),MyServices.class);
                stopService(intent);
            }
        });

    }
}

MyServices.Java
---------------------------
package com.android.anil.services;

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

/**
 * Created by Anil  on 11/23/15.
 */
public class MyServices extends Service {
   
@Nullable
    @Override
   
public IBinder onBind(Intent intent) {
       
return null;
    }

   
@Override
   
public void onCreate() {
        Toast.makeText(MyServices.
this, "Services is Created()", Toast.LENGTH_SHORT).show();
       
super.onCreate();
    }

   
@Override
   
public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(MyServices.
this, "Services is Running.........", Toast.LENGTH_SHORT).show();
       
return super.onStartCommand(intent, flags, startId);
    }

   
@Override
   
public void onDestroy() {
        Toast.makeText(MyServices.
this, "Services is Stopped", Toast.LENGTH_SHORT).show();
       
super.onDestroy();
    }
}
AndroidManifest.xml
------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.android.anil.services">

    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">
        <
activity
           
android:name=".MainActivity"
           
android:label="@string/app_name"
           
android:theme="@style/AppTheme.NoActionBar">
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />

                <
category android:name="android.intent.category.LAUNCHER" />
            </
intent-filter>
        </
activity>
        <service android:name=".MyServices"></service>
    </
application>

</
manifest>


Output is:
---------------------------










Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...