Implementasi Broadcast Receiver Android
- Buat splash_screen.xml sebagai tampilan awal seperti dibawah ini:
<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:animateLayoutChanges=“true” >
<ImageView
android:id=“@+id/logo”
android:layout_width=“120dp”
android:layout_height=“120dp”
android:layout_centerHorizontal=“true”
android:layout_centerVertical=“true” />
<RelativeLayout
android:layout_width=“135dp”
android:layout_height=“wrap_content”
android:layout_below=“@+id/logo”
android:gravity=“center”
android:layout_centerHorizontal=“true”
android:animateLayoutChanges=“true” >
<TextView
android:id=“@+id/t1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content” />
<TextView
android:id=“@+id/t2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t1” />
<TextView
android:id=“@+id/t3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t2” />
<TextView
android:id=“@+id/t4”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t3” />
<TextView
android:id=“@+id/t5”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t4” />
<TextView
android:id=“@+id/t6”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t5” />
<TextView
android:id=“@+id/t7”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t6” />
<TextView
android:id=“@+id/t8”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t7” />
<TextView
android:id=“@+id/t9”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t8” />
<TextView
android:id=“@+id/t10”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t9” />
<TextView
android:id=“@+id/t11”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t10” />
<TextView
android:id=“@+id/t12”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t11” />
<TextView
android:id=“@+id/t13”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t12” />
<TextView
android:id=“@+id/t14”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t13” />
<TextView
android:id=“@+id/t15”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t14” />
<TextView
android:id=“@+id/t16”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t15” />
<TextView
android:id=“@+id/t17”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t16” />
<TextView
android:id=“@+id/t18”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t17” />
<TextView
android:id=“@+id/t19”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t18” />
<TextView
android:id=“@+id/t20”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/t19” />
</RelativeLayout>
</RelativeLayout>
- Buat activity SplashScreen.java seperti dibawah ini:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
public class SplashScreen extends Activity {
private ImageView logo;
private Animation anim1;
private SharedPreferences pref;
private Handler handlerTimer = new Handler();
private TextView t1, t2, t3, t4, t5, t6, t7, t8, t9, t10,
t11, t12, t13, t14, t15, t16, t17, t18, t19, t20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_screen);
init();
initAnimation();
}
private void init() {
logo = (ImageView) findViewById(R.id.logo);
t1 = (TextView) findViewById(R.id.t1);
t2 = (TextView) findViewById(R.id.t2);
t3 = (TextView) findViewById(R.id.t3);
t4 = (TextView) findViewById(R.id.t4);
t5 = (TextView) findViewById(R.id.t5);
t6 = (TextView) findViewById(R.id.t6);
t7 = (TextView) findViewById(R.id.t7);
t8 = (TextView) findViewById(R.id.t8);
t9 = (TextView) findViewById(R.id.t9);
t10 = (TextView) findViewById(R.id.t10);
t11 = (TextView) findViewById(R.id.t11);
t12 = (TextView) findViewById(R.id.t12);
t13 = (TextView) findViewById(R.id.t13);
t14 = (TextView) findViewById(R.id.t14);
t15 = (TextView) findViewById(R.id.t15);
t16 = (TextView) findViewById(R.id.t16);
t17 = (TextView) findViewById(R.id.t17);
t18 = (TextView) findViewById(R.id.t18);
t19 = (TextView) findViewById(R.id.t19);
t20 = (TextView) findViewById(R.id.t20);
}
private void initAnimation() {
anim1 = AnimationUtils.loadAnimation(this, R.anim.logo);
Thread animated = new Thread() {
public void run() {
logo.setImageResource(R.drawable.logo);
logo.startAnimation(anim1);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t1.setText(“I”);
t1.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1000);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t2.setText(“n”);
t2.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1050);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t3.setText(“t”);
t3.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1100);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t4.setText(“e”);
t4.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1150);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t5.setText(“r”);
t5.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1200);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t6.setText(“a”);
t6.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1250);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t7.setText(“c”);
t7.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1300);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t8.setText(“t”);
t8.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1350);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t9.setText(“i”);
t9.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1400);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t10.setText(“v”);
t10.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1450);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t11.setText(“e “);
t11.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1500);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t12.setText(“D”);
t12.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1550);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t13.setText(“e”);
t13.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1600);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t14.setText(“v”);
t14.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1650);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t15.setText(“e”);
t15.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1700);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t16.setText(“l”);
t16.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1750);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t17.setText(“o”);
t17.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1800);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t18.setText(“p”);
t18.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1850);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t19.setText(“e”);
t19.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1900);
handlerTimer.postDelayed(new Runnable() {
public void run() {
t20.setText(“r”);
t20.setAnimation(AnimationUtils.loadAnimation(SplashScreen.this, android.R.anim.slide_in_left));
}
}, 1950);
handlerTimer.postDelayed(new Runnable() {
public void run() {
}
}, 2000);
handlerTimer.postDelayed(new Runnable() {
public void run() {
pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean passEnabled = pref.getBoolean(“passEnabled”, true);
if (passEnabled) {
Intent change = new Intent(SplashScreen.this, Auth.class);
startActivity(change);
} else if (!passEnabled) {
Intent change = new Intent(SplashScreen.this, Main.class);
startActivity(change);
}
finish();
}
}, 4000);
}
};
animated.start();
}
}
- Buat tampilan auth.xml seperti dibawah ini:
<?xml version=“1.0” encoding=“utf-8”?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:animateLayoutChanges=“true” >
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_centerHorizontal=“true”
android:animateLayoutChanges=“true”
android:padding=“10dp” >
<EditText
android:id=“@+id/password1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_below=“@+id/text”
android:drawableLeft=“@drawable/key”
android:ems=“10”
android:inputType=“textPassword”
android:layout_marginBottom=“5dp” >
</EditText>
<TextView
android:id=“@+id/text”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Input your password” />
<ImageView
android:id=“@+id/ok”
android:layout_width=“wrap_content”
android:layout_height=“25dp”
android:layout_centerHorizontal=“true”
android:layout_below=“@+id/password1”
android:src=“@drawable/ok_b” />
</RelativeLayout>
</RelativeLayout>
- Buat activity Auth.java seperti dibawah ini:
package com.interactive.i_sms;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Auth extends Activity {
private EditText pass;
private ImageView ok;
private DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auth);
ok = (ImageView) findViewById(R.id.ok);
pass = (EditText) findViewById(R.id.password1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor get = db.getAll();
get.moveToFirst();
if (get.getCount() == 0) {
db.insert(pass.getText().toString());
Intent change = new Intent(Auth.this, Main.class);
startActivity(change);
finish();
} else {
if (pass.getText().toString().equals(db.getPassword(get))) {
Intent change = new Intent(Auth.this, Main.class);
startActivity(change);
finish();
} else {
Toast.makeText(Auth.this, “The password is incorrect !”,
Toast.LENGTH_LONG).show();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuItem = getMenuInflater();
menuItem.inflate(R.menu.options_auth, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:
finish();
break;
}
return false;
}
}
- Buat service ServiceRecv.java seperti dibawah ini:
package com.interactive.i_sms;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ServiceRecv extends Service {
private class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String strMessage = “”;
if (extras != null) {
Object[] smsextras = (Object[]) extras.get(“pdus”);
for (int i = 0; i < smsextras.length; i++) {
SmsMessage smsmsg = SmsMessage
.createFromPdu((byte[]) smsextras[i]);
String strMsgBody = smsmsg.getMessageBody().toString();
String strMsgSrc = smsmsg.getOriginatingAddress();
strMessage += “SMS from ” + strMsgSrc + ” : ” + strMsgBody;
}
Bundle send = new Bundle();
send.putString(“message”, strMessage);
Intent change = new Intent(ServiceRecv.this, Alert.class);
change.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
change.putExtras(send);
startActivity(change);
}
}
}
private SMSReceiver smsRecv;
private IntentFilter intentFilter;
@Override
public void onCreate() {
super.onCreate();
smsRecv = new SMSReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction(“android.provider.Telephony.SMS_RECEIVED”);
registerReceiver(smsRecv, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(smsRecv);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
- Buat kelas DatabaseHandler.java untuk menghandle database seperti dibawah ini:
package com.interactive.i_sms;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME = “0001.db”;
private static final int SCHEMA_VERSION = 6;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(“CREATE TABLE PASSWORD (ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, PASSWORD VARCHAR NOT NULL)”);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insert(String password) {
ContentValues carry = new ContentValues();
carry.put(“PASSWORD”, password);
getWritableDatabase().insert(“PASSWORD”, null, carry);
}
public void update(String password) {
ContentValues carry = new ContentValues();
carry.put(“PASSWORD”, password);
getWritableDatabase().update(“PASSWORD”, carry, null, null);
}
public Cursor getAll() {
return (getReadableDatabase().rawQuery(“SELECT * FROM PASSWORD”, null));
}
public String getPassword(Cursor c) {
return (c.getString(1));
}
}
- Buat tampilan halaman pengaturan prefs.xml seperti dibawah ini:
<?xml version=“1.0” encoding=“utf-8”?>
<PreferenceScreen
xmlns:android=“http://schemas.android.com/apk/res/android” >
<CheckBoxPreference
android:title=“Password Authentication”
android:summaryOn=“Password is enabled”
android:summaryOff=“Password is disabled”
android:defaultValue=“true”
android:key=“passEnabled” />
<EditTextPreference
android:title=“Setup new password”
android:summary=“input your new password”
android:key=“newPass” />
</PreferenceScreen>
- Buat preference activity Prefs.java seperti dibawah ini:
package com.interactive.i_sms;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class Prefs extends PreferenceActivity{
private SharedPreferences pref;
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
}
@Override
protected void onDestroy() {
super.onDestroy();
String newPass = pref.getString(“newPass”, “”);
Cursor get = db.getAll();
get.moveToFirst();
if (!db.getPassword(get).equals(newPass)) {
if (!newPass.equals(“”)) {
db.update(newPass);
}
}
}
}
- Buat tampilan main.xml seperti dibawah ini:
<?xml version=“1.0” encoding=“utf-8”?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_margin=“10dp” >
<TextView
android:id=“@+id/passT”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:layout_alignParentTop=“true”
android:text=“Password : “ />
<TextView
android:id=“@+id/tes”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@+id/passT” />
<TextView
android:id=“@+id/tes1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:layout_below=“@+id/passT”
android:text=“You will get alert when receive an sms message…” />
<TextView
android:id=“@+id/t1es”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:layout_below=“@+id/tes1”
android:text=“You may close this application” />
<ImageView
android:id=“@+id/end”
android:layout_width=“wrap_content”
android:layout_height=“25dp”
android:layout_centerHorizontal=“true”
android:layout_below=“@+id/t1es”
android:layout_marginTop=“5dp”
android:src=“@drawable/end_b” />
</RelativeLayout>
- Buat activity Main.java seperti dbawah ini:
package com.interactive.i_sms;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
private SharedPreferences pref;
private TextView tes;
private ImageView end;
private Intent smsReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
smsReceiver = new Intent(this, ServiceRecv.class);
startService(smsReceiver);
pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());;
tes = (TextView) findViewById(R.id.tes);
end = (ImageView) findViewById(R.id.end);
end.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(smsReceiver);
Toast.makeText(Main.this, “SMS popup service has been stopped”, Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onResume() {
super.onResume();
if (pref.getBoolean(“passEnabled”, true)) {
tes.setText(“Enabled”);
} else if (!pref.getBoolean(“passEnabled”, true)) {
tes.setText(“Disabled”);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuItem = getMenuInflater();
menuItem.inflate(R.menu.options_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.preferences :
Intent prefs = new Intent(Main.this, Prefs.class);
startActivity(prefs);
break;
case R.id.exit :
finish();
break;
}
return false;
}
}
Screenshot