i am doing an sms hiding project which is broadcast receiver the code is given below
package com.sms.sms;
public class ReceiverClass extends BroadcastReceiver
{
SQLiteDatabase DiaryDB = null;
String message,number;
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[ ] msgs = null;
String str = "";
if (bundle != null)
{
abortBroadcast();
//---retrieve the received message here ---
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 += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
message = msgs[i].getMessageBody().toString();
number = msgs[i].getOriginatingAddress();
}
// ........first show sms here.....
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
String name = findNameByAddress(context, number);
if(name.equals(number))
name = "Unknown";
DiaryDB = context.openOrCreateDatabase("DIARY_DATABASE", context.MODE_PRIVATE, null);
DiaryDB.execSQL("CREATE TABLE IF NOT EXISTS Messages (TIMESTAMP DATE DEFAULT (DATETIME('now','localtime')), MESSAGE varchar, SENDER varchar, NAME varchar);");
System.out.println("table createdddddddddddddddddddddddddd");
DiaryDB.execSQL("INSERT INTO Messages(MESSAGE,SENDER,NAME) VALUES('" + message +"','"+ number +"','"+ name +"')");
DiaryDB.close();
updateName(context,name, number);
}
}
public String findNameByAddress(Context ct,String address)
{
Uri myPerson = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(address));
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = ct.getContentResolver().query(myPerson, projection, null, null, null);
if (cursor.moveToFirst())
{
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("","Found contact name");
cursor.close();
return name;
}
cursor.close();
Log.e("","Not Found contact name");
return address;
}
public void updateName(Context ct, String name, String sender)
{
DiaryDB = ct.openOrCreateDatabase("DIARY_DATABASE", ct.MODE_PRIVATE, null);
DiaryDB.execSQL("UPDATE Messages SET NAME='"+name+"' WHERE SENDER='"+sender+"'");
DiaryDB.close();
}
I had two button ON and OFF in my main Activity. What I need is that when i press the ON button it should Start the Bordcast receiver and should start hide sms and when i press the OFF button i need to stop (Broadcast receiver)hiding messages process or should get sms back to the inbox*(which will occur when the broadcast receiver get turned off)*. now how can i on and off a Boardcast receiver process please help
edition after answer
my activity class//
package com.an.oid;
public class OnoffActivity extends Activity {
int count =0;
Button a,b;
ReceiverClass rc ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a=(Button)findViewById(R.id.button1);
b=(Button)findViewById(R.id.button2);
rc= new ReceiverClass();
a.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
System.out.println("onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
registerReceiver(rc,filter);
}
});
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
unregisterReceiver(rc);
}
});
}
}
my manifest//
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.an.oid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".OnoffActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
After getting anser i tried this but it is not working?
This is only possible if you register your receiver at Activity level(not Manifest) using
and on the click of the button you can Unregister it using..
Edit II
//register button
//Unregister button