Friday, 31 May 2013

SIMPLE PROGRAMME ON PENDINGINTENT IN ANDROID


 PendingintentActivity.java

package com.mypendingintent;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PendingintentActivity extends Activity {
    /** Called when the activity is first created. */
    EditText et1;
    Button b1;
    String s1;
    long t1, t2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et1 = (EditText) findViewById(R.id.editText1);
        b1 = (Button) findViewById(R.id.button1);
    }

    public void start(View v) {
         AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
         s1=et1.getText().toString();
         t1=Integer.parseInt(s1);
         long t2=System.currentTimeMillis()+(t1*1000);
         Intent i=new Intent(this,Next2.class);
         PendingIntent pi=PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);
         am.set(AlarmManager.RTC_WAKEUP, t2, pi);
         Toast.makeText(getBaseContext(), "alarm ready", 2000).show();
        
    }
}

Next2,java

package com.mypendingintent;

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


public class Next2 extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"alarm mogindi",3000).show();
   
       
    }

}


Manifest.xml


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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PendingintentActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

WHAT IS A PENDING INTENT IN ANDROID

PENDING INTENT IS A  EXPLICIT INTENT ,,,,,,,,

WE ARE USING THIS INTENT INVOKING THE ACTIONS FOR LATER TIME ,,,,,


 PendingintentActivity.java

package com.mypendingintent;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PendingintentActivity extends Activity {
    /** Called when the activity is first created. */
    EditText et1;
    Button b1;
    String s1;
    long t1, t2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et1 = (EditText) findViewById(R.id.editText1);
        b1 = (Button) findViewById(R.id.button1);
    }

    public void start(View v) {
         AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
         s1=et1.getText().toString();
         t1=Integer.parseInt(s1);
         long t2=System.currentTimeMillis()+(t1*1000);
         Intent i=new Intent(this,Next2.class);
         PendingIntent pi=PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);
         am.set(AlarmManager.RTC_WAKEUP, t2, pi);
         Toast.makeText(getBaseContext(), "alarm ready", 2000).show();
        
    }
}

Next2,java

package com.mypendingintent;

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


public class Next2 extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"alarm mogindi",3000).show();
   
       
    }

}


Manifest.xml


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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PendingintentActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

SIMPLE PROGRAMME ON SMS BROADCAST RECVER IN ANDROID

 SMSReceiver.Java


package com.broadsmsrx;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {
    private NotificationManager mManager;
    Cursor c;
    Context context1;

    @Override
    public void onReceive(Context context, Intent intent) {
        context1=context;

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[])bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            }
            if (messages.length > -1) {
                Log.i("TAG###################################", "Message recieved: " + messages[0].getMessageBody());

                Toast toast = Toast.makeText(context,
                        "from: "+getContactNameFromNumber(messages[0].getOriginatingAddress().toString()).toString()+": " +messages[0].getMessageBody(), Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
    private String getContactNameFromNumber(String number) {
        // define the columns I want the query to return
        String conname=number;
        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
        Cursor c = context1.getContentResolver().query(lookupUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null); 
        while(c.moveToNext()){  
            /* If we find a match we put it in a String.*/   
            conname = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        }
        return conname;
    }
}


manifest.xml

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SMSBraodCastReceiverExActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-permission android:name="android.permission.READ_CONTACTS"  />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />       
    </application>

</manifest>

SAMPLE PROGRAMME ON IMPLICIT SERVICE IN ANDROID

package com.implicitservise;

import android.app.Activity;
import android.content.Context;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.TextView;

public class ImplicitserviceActivity extends Activity {
    TextView tv;
    String s;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textview1);
    }

    public void cell(View v) {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        s = tm.getDeviceId().toString();
        tv.setText(s);
    }

    public void vibrat(View v) {
        Vibrator v1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v1.vibrate(5000);

    }

    public void ring(View v) {
        RingtoneManager rm = (RingtoneManager) getSystemService(Context.RINGTONE_SERVICE);
    }

}
MANIFESTFILE.XML:

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ImplicitserviceActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

SIMPLE PROGRAMME ON EXPLICIT SERVICE IN ANDROID

ExplicitServiceActivity.java

package com.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ExplicitServiceActivity extends Activity
implements OnClickListener {
    /** Called when the activity is first created. */
     Button bstart,bstop;
    @Override  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bstart=(Button)findViewById(R.id.button1);
        bstop=(Button)findViewById(R.id.button2);
        bstart.setOnClickListener(this);
        bstop.setOnClickListener(this);
       
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.button1){
            startService(new Intent(this,MyService.class));
        }else{
            stopService(new Intent(this,MyService.class));
        }
    }
   
}




Myservice.java



package com.service;

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 arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Created...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed...",
                Toast.LENGTH_LONG).show();
    }


}







Manifest.xml

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".MyService">           
        </service>
        <activity
            android:name=".ExplicitServiceActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>







EXAMPLE PROGRAMME ON SERVICE IN ANDROID

ExplicitServiceActivity.java

package com.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ExplicitServiceActivity extends Activity
implements OnClickListener {
    /** Called when the activity is first created. */
     Button bstart,bstop;
    @Override  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bstart=(Button)findViewById(R.id.button1);
        bstop=(Button)findViewById(R.id.button2);
        bstart.setOnClickListener(this);
        bstop.setOnClickListener(this);
       
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.button1){
            startService(new Intent(this,MyService.class));
        }else{
            stopService(new Intent(this,MyService.class));
        }
    }
   
}




Myservice.java



package com.service;

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 arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Created...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed...",
                Toast.LENGTH_LONG).show();
    }


}







Manifest.xml

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".MyService">           
        </service>
        <activity
            android:name=".ExplicitServiceActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>







WHAT IS A SERVICE IN ADROID

SERVICE IS A CLASS IN ANDROID,,,,,IT IS RUNNING IN THE BACKGROUND FOR THE ANDROID APPLICATION....

SERVICES ARE TWO TYPES THEY ARE

IMPLCIT SERVICES...

EXPLICIT SERVICES.....


IMPLICIT SERVICES:----

IMPLICIT SERVICES THAT ARE ALLOWED BY SERVICEMANAGER.......

TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

EXPLICIT SERVICES:-----

EXPLICIT SERVICES ARE ALLOWE WITH EXTENDS WITH THE NAME Service..




WHEN WE ARE EXTENDS WITH THE SERVICE ,,,,,,,


WE NEED TO REGISTER THESE SERVICES WITH THE <service></service>


ExplicitServiceActivity.java

package com.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ExplicitServiceActivity extends Activity
implements OnClickListener {
    /** Called when the activity is first created. */
     Button bstart,bstop;
    @Override  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bstart=(Button)findViewById(R.id.button1);
        bstop=(Button)findViewById(R.id.button2);
        bstart.setOnClickListener(this);
        bstop.setOnClickListener(this);
       
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.button1){
            startService(new Intent(this,MyService.class));
        }else{
            stopService(new Intent(this,MyService.class));
        }
    }
   
}


Myservice.java

package com.service;

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 arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Created...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started...",
                Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed...",
                Toast.LENGTH_LONG).show();
    }


}







Manifest.xml





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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".MyService">           
        </service>
        <activity
            android:name=".ExplicitServiceActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>