Hello Friends'
Here i will show you a very simple example in android about registering and unregistered BroadcastReceiver through code.
In this simple app i put two button
one for registering receiver and other for unregistered.
The sample layout code is as follow...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<Button
android:id="@+id/registerButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Receiver" />
<Button
android:id="@+id/unRegisterButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="UnRegister Receiver" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wait for message to come"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Now lets see the SMSReceiver class....
package in.blogspot.longjamcode;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
//a boolean variable to check whether receiver is register or not
public static boolean isReceiverRegister;
private static SMSReceiver receiver;
//making constructor private so that it can't call from outside this class
private SMSReceiver(){}
//a method to get object of SMSReceiver class
public static SMSReceiver getSMSReceiver() {
if(receiver == null) {
receiver = new SMSReceiver();
return receiver;
}
else
return receiver;
}
//this method will call whenever a new message is received
public void onReceive(final Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String message = currentMessage.getDisplayMessageBody();
Toast.makeText(context, ""+message, Toast.LENGTH_LONG).show();
}
}
}
//method to register receiver
public static void registerBroadcastReceiver (Context context, BroadcastReceiver receiver) {
if(!SMSReceiver.isReceiverRegister) {
SMSReceiver.isReceiverRegister = true;
context.registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
Toast.makeText(context, "Receiver is Register", Toast.LENGTH_SHORT).show();
}
}
//method to unregistered receiver
public static void unRegisterBroadcastReceiver (Context context, BroadcastReceiver receiver) {
if(SMSReceiver.isReceiverRegister) {
SMSReceiver.isReceiverRegister = false;
context.unregisterReceiver(receiver);
Toast.makeText(context, "Receiver is UnRegister", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity java file will like the following...
package in.blogspot.longjamcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button registerReceiver, unRegisterReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver = (Button)findViewById(R.id.registerButton);
unRegisterReceiver = (Button)findViewById(R.id.unRegisterButton);
registerReceiver.setTag("register");
unRegisterReceiver.setTag("unregister");
registerReceiver.setOnClickListener(buttonListener);
unRegisterReceiver.setOnClickListener(buttonListener);
}
private OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag().equals("register"))
SMSReceiver.registerBroadcastReceiver(MainActivity.this, SMSReceiver.getSMSReceiver());
else
SMSReceiver.unRegisterBroadcastReceiver(MainActivity.this, SMSReceiver.getSMSReceiver());
}
};
}
Lastly Manifest file...
In this manifest we are not registering receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.blogspot.longjamcode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
give this two uses permission
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<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/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The output will be like...
Full source code download click here
Here i will show you a very simple example in android about registering and unregistered BroadcastReceiver through code.
In this simple app i put two button
one for registering receiver and other for unregistered.
The sample layout code is as follow...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<Button
android:id="@+id/registerButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Receiver" />
<Button
android:id="@+id/unRegisterButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="UnRegister Receiver" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wait for message to come"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Now lets see the SMSReceiver class....
package in.blogspot.longjamcode;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
//a boolean variable to check whether receiver is register or not
public static boolean isReceiverRegister;
private static SMSReceiver receiver;
//making constructor private so that it can't call from outside this class
private SMSReceiver(){}
//a method to get object of SMSReceiver class
public static SMSReceiver getSMSReceiver() {
if(receiver == null) {
receiver = new SMSReceiver();
return receiver;
}
else
return receiver;
}
//this method will call whenever a new message is received
public void onReceive(final Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String message = currentMessage.getDisplayMessageBody();
Toast.makeText(context, ""+message, Toast.LENGTH_LONG).show();
}
}
}
//method to register receiver
public static void registerBroadcastReceiver (Context context, BroadcastReceiver receiver) {
if(!SMSReceiver.isReceiverRegister) {
SMSReceiver.isReceiverRegister = true;
context.registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
Toast.makeText(context, "Receiver is Register", Toast.LENGTH_SHORT).show();
}
}
//method to unregistered receiver
public static void unRegisterBroadcastReceiver (Context context, BroadcastReceiver receiver) {
if(SMSReceiver.isReceiverRegister) {
SMSReceiver.isReceiverRegister = false;
context.unregisterReceiver(receiver);
Toast.makeText(context, "Receiver is UnRegister", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity java file will like the following...
package in.blogspot.longjamcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button registerReceiver, unRegisterReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver = (Button)findViewById(R.id.registerButton);
unRegisterReceiver = (Button)findViewById(R.id.unRegisterButton);
registerReceiver.setTag("register");
unRegisterReceiver.setTag("unregister");
registerReceiver.setOnClickListener(buttonListener);
unRegisterReceiver.setOnClickListener(buttonListener);
}
private OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag().equals("register"))
SMSReceiver.registerBroadcastReceiver(MainActivity.this, SMSReceiver.getSMSReceiver());
else
SMSReceiver.unRegisterBroadcastReceiver(MainActivity.this, SMSReceiver.getSMSReceiver());
}
};
}
Lastly Manifest file...
In this manifest we are not registering receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.blogspot.longjamcode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
give this two uses permission
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<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/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The output will be like...
Full source code download click here
Let you share it on facebook.
ReplyDelete