Sunday, April 1, 2012

Start BroadcastReceiver from Activity

Some time we need to start a service from Android Activity..
how can we achieve this i am going to write step by step.

We will create very simple app to do this work
 -------------------------------------------
App Name: Activity2BReceiver
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------


MyActivity.java
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("Debug", "Activity has been started..");
}
}

Then Create a Broadcast Receiver

MyReceiver.java
  
package com.rdc;

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

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.v("Debug", "SMS Broadcast Receiver has been started..");
Toast.makeText(context, "BReceiver is watching ur message..",
Toast.LENGTH_SHORT).show();

}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


Don't forget to make entry in "Manifest" file for Receiver
  
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MyActivity"
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="com.rdc.MyReceiver">

<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

</application>
</manifest>

Run this app and send message from another mobile/emulator, Receiver will notify you about new sms
See below output screens tensted on Emulator and Real Device


You can download the complete source code zip file here : Activity2BReceiver


I'd love to hear your thoughts!

Labels: , ,

Tuesday, February 28, 2012

Start Service from Broadcast Receiver

Some time we need to start Service from Broadcast Receiver..
how can we achieve this i am going to write step by step.

So lets create a small App to do this things 

-------------------------------------------
App Name: BReceiver2Service
Package Name: com.rdc
Android SDK: Android SDK 2.2 / API 8
-------------------------------------------

MyReceiver.java 
package com.rdc;

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

public class MyReceiver extends BroadcastReceiver {

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

Toast.makeText(context, "MyReceiver Started",
Toast.LENGTH_SHORT).show();
Log.v("Info Message", "in Broadcast receiver");
Intent myIntent=new Intent(context,MyService.class);
context.startService(myIntent);
}

}


MyService
package com.rdc;

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) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getBaseContext(), "Service Started",
Toast.LENGTH_SHORT).show();
/* We want this service to continue running until it is explicitly
stopped, so return sticky. */
return START_STICKY;
}
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>


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

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<service
android:enabled="true"
android:name=".MyService">
<intent-filter>
<action android:name="com.rdc.MyService">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>

</manifest>

Now Reboot Emulator, Toast will appear.

you can download zip file from here : BReceiver2Service

cheers!!

 I'd love to hear your thoughts!!

Labels: , ,

Monday, February 6, 2012

Start Activity from Broadcast Recevier


Some time we need to start an Activity from Broadcast Receiver..
how can we achieve this i am going to write step by step.

So lets create a small App to do this things 

-------------------------------------------
App Name: BReceiver2Activity
Package Name: com.rdc
Android SDK: Android SDK 2.2 / API 8
-------------------------------------------

MyReceiver.java
package com.rdc;

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

public class MyReceiver extends BroadcastReceiver {

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

Toast.makeText(context, "MyReceiver Started",
Toast.LENGTH_SHORT).show();
Log.v("Info Message", "in Broadcast receiver");
Intent myIntent=new Intent(context,MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}

}



MyActivity.java
package com.rdc;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_weight="0.14" />
</LinearLayout>


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

<application
android:icon="@drawable/icon"
android:label="@string/app_name">

<activity
android:enabled="true"
android:name=".MyActivity">
<intent-filter>
<action android:name="com.rdc.MyActivity">
</action>
</intent-filter>
</activity>

<receiver
android:enabled="true"
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

</application>
</manifest>

Now Reboot Emulator, Activity will appear on start-up.

You can download source code here: BReceiver2Activity

cheers!!

I'd love to hear your thoughts!!

Labels: , ,

Saturday, October 8, 2011

Listening incoming sms message in Android

The following code shows "how to get notification when android mobile receive a message"

We will use only Broadcast Receiver's method not any URI query or something else.


So let's create a simple android app, then delete default Activity and create new java class
which extends Broadcast-receiver  look like this

SMSNotification.java
  
package com.rdc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class SMSNotification extends BroadcastReceiver {

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

Toast.makeText(context,"Wow! we got a Message :)",Toast.LENGTH_SHORT).show();

Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if(bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length;i++){
msgs[i]= SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from: " +msgs[i].getOriginatingAddress();
str +="\n"+"Message is:";
str += msgs[i].getMessageBody().toString();
str +="\n";
}

Log.v("Debug", str);
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}


AndroidManifest.xml
  
























output is like this

cheers!!

I'd love to hear your thoughts!

Labels: , ,