Android Sim Tracker code - android

Hie..
I am making an app in which I am detecting whether SIM has changed or not..?
On installation of my app I am storing the current SIM serial number in shared preferences.
On boot I have registered a broadcast receiver. In onReceive() method I am accessing current SIM serial number and comparing it with the stored one on the time of installation,
Below is my code of receiver :
package com.secuirity.sms;
import org.apache.harmony.xnet.provider.jsse.GMailSender;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class BootUpReciever extends BroadcastReceiver{
Context context;
String email;
String currentSimSerial;
SharedPreferences settings;
SmsManager smsMgr = SmsManager.getDefault();
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onReceive(Context context, Intent intent) {
settings = context.getSharedPreferences(PREFS_NAME, 0);
String storedSimSerial = settings.getString("storedSimSerial", null);
Log.d("Stored Sim Serial::",storedSimSerial);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
currentSimSerial = tm.getSimSerialNumber();
Log.d("Current Sim Serial","::"+currentSimSerial);
String trustedNum = settings.getString("cellno", null);
email = settings.getString("email", null);
if(currentSimSerial == storedSimSerial){
}else{
Log.d("Sim changed","!!!");
new GmailAsync().execute("");
String sms = "Sim card has changed, " +
"Sim Serial Number of this card is\n"+currentSimSerial+
"Network Operator"+tm.getNetworkOperatorName();
smsMgr.sendTextMessage(trustedNum, null,sms, null, null);
}
Intent sms = new Intent(context, SMSReceiver.class);
sms.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(sms);
Intent netAvailability = new Intent(context, CheckingNetworkAvailability.class);
netAvailability.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(netAvailability);
}
public class GmailAsync extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String mail_body = "Sim serial number is "+currentSimSerial;
String subject = "Your Sim has changed!!!";
GMailSender sender = new GMailSender("securemob.viasms#gmail.com", "smsfun890");
try {
sender.sendMail(subject,
mail_body+"\nThanks for using Mobile Security App",
"securemob.viasms#gmail.com",
email,null);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d("Mail","Sent");
}
}
}
But Its not working.... :(:(:(
I have the following permissions READ_PHONE_STATE" and "RECEIVE_BOOT_COMPLETED"

I cant see anything wrong in your code, but the first thing I would check is that you have the permission to read the SIM card set in the Manifest.

Hie Guys I got the solution of BOOT UP Receiver....
When android mobile boots then this event is broadcasts to every app, But app those are installed in the internal memory responds soon as compared to the app stored on SD card.
Hence solution was
android:installLocation="internalOnly"
In manifest file as manifest attribute.

Related

why is my SMS verification code not working properly?

I think I'm in a bit of a catch 22 situation. I'm making an app that verifies the user's phone number by sending a text message to the number the user entered and then checking - that way it's definitely the user's phone number. This activity should only appear once, at the beginning - for the user to verify, and from then on activity 2 should be loaded.
I am trying to do this with :
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
But it doesn't seem to be working properly. For a start, I'm not even sure it should be null. When I turn off my phone and on again, it starts back at activity 1, asking for verification.
And I can't test it. I need to run the app on my device as I can't send an SMS from the emulator, and I'm unable to get to the files where sharedPreferences exist on my device, so I can't even see if it's being written correctly.
Any advice on how I might be able to see the MyData.xml on my phone and proceed with testing my app, or tell my how I may be able to improve my code ? I downloaded the Astro app but still couldn't find it.
Here's my code :
package com.example.chris.tutorialspoint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.tutorialspoint.R;
public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
String phoneNo;
String origNumber;
private BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
//if it is not in there, proceed with phone number verification
else {
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
IntentFilter filter = new IntentFilter();
// the thing we're looking out for is received SMSs
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
origNumber = msg.getOriginatingAddress();
Toast.makeText(getApplicationContext(), "Originating number" + origNumber, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Sent to number" + phoneNo, Toast.LENGTH_LONG).show();
}
};
registerReceiver(receiver, filter);
}
}
protected void sendSMSMessage() {
phoneNo = txtphoneNo.getText().toString();
//this is the SMS received
String message = "Verification test code. Please ignore this message. Thank you.";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
//if originating phone number is the same as the sent to number, save
//and go to the next activity
if (origNumber.equals(phoneNo)) {
//save the phone number
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phonenumber", phoneNo);
editor.commit();
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}
What do you expect to happen here:
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
...
}
The method that you are calling, getString(String, String), uses the second parameter as the default value, that is, if no property is found, it will default to, as you have defined, an empty string, "".
You then try and compare this to null, which will never be equal.
Try changing it up to something like this:
String phoneNoCheck = sharedPreferences.getString("phonenumber", null);
// check if null or empty
if ( null == phoneNoCheck || phoneNoCheck.equals("") ) {
// unregistered
}

Browser.BOOKMARKS_URI is no more?

I am trying to add content observer for AOSP Browser's history provider i.e., with Uri Browser.BOOKMARKS_URI. If I attach an observer, the onChange(boolean) gets called on my ICS running my Samsung GT-S7562, my JB running Samsung GT-I8262 or HTC Desire X but I don't get any notifications for AOSP Browser provider on my friend's Android 4.3 running Samsung SM-G7102. However for Google Chrome's provider, i.e., residing at content://com.android.chrome.browser/bookmarks, I get notified for changes on all android releases. Also if I query the AOSP Browser database & scan for entries, it only returns those opened in Chrome (I mean it returns Chrome's history in Browser.BOOKMARKS_URI provider).
Below is the sample service I tested for reference which is not actually required (don't mind but I think everything is quite clear above already):
package vpz.hp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class Watcher extends Service {
private Provider Browser;
private Provider Chrome;
#Override public IBinder onBind(Intent intent) {
return null;
}
#Override public void onDestroy() {
if (this.Browser != null)
this.Browser.Stop();
if (this.Chrome != null)
this.Chrome.Stop();
super.onDestroy();
}
public final int onStartCommand(Intent Intention, int StartId, int Flags) {
this.Browser = new Provider(super.getApplicationContext(), android.provider.Browser.BOOKMARKS_URI);
this.Browser.Start();
if (Search(getApplicationContext(), "com.android.chrome")) {
this.Chrome = new Provider(super.getApplicationContext(), Uri.parse("content://com.android.chrome.browser/bookmarks"));
this.Chrome.Start();
}
return START_STICKY;
}
private static final boolean Search(Context Sender, String Package) {
try {
return Sender.getPackageManager().getPackageInfo(Package, PackageManager.GET_ACTIVITIES) != null;
} catch (NameNotFoundException Error) {
return false;
}
}
private static class Provider extends ContentObserver {
private Context Sender;
private Uri URI;
public Provider(Context Sender, Uri URI) {
super(new Handler());
this.Sender = Sender;
this.URI = URI;
}
#Override public void onChange(boolean Self) {
String Message = this.URI.toString() + " onChange(" + Self + ")";
Toast.makeText(this.Sender, Message, Toast.LENGTH_SHORT).show();
Log.e("VPZ", Message);
}
public final void Start() {
this.Sender.getContentResolver().registerContentObserver(this.URI, true, this);
}
public final void Stop() {
this.Sender.getContentResolver().unregisterContentObserver(this);
}
}
}
I have added below permission(s) already in the manifest:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
Is content observer on AOSP Browser has been deprecated?

ending call in android using telephonyserivce.endcall()

I am working on app where i want to end outgoing call.
this is the main class
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class phonecalls extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
call();
}
private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("dialing-example", "Call failed", activityException);
}
}
}
and outgoingclass is this
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.Method;
public class OutgoingCallReceiver extends BroadcastReceiver {
private final String TAG = "CallControl";
Context context;
String incomingNumber;
ITelephony telephonyService;
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE);
try {
// Java reflection to gain access to TelephonyManager's
// ITelephony getter
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService=(ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
}
}
But the call is not ended even though i am able to print no. when outgoing call starts which show broadcasting receiver is working right .
Any suggestion
Yes definately there is two way to do it either using airplane mode or internal telephony for this . I used internal telephony and code I developed is uploaded here https://github.com/deependersingla/end_calls.
If you need anything more tell me , will be happy to help.

Call Control Example Issue on Emulator

I am testing call control example given on: http://prasanta-paul.blogspot.com/2010/09/call-control-in-android.html on emultaor.
Code is as below:
package com.example.callblock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
public class CallBlock extends BroadcastReceiver{
String[] blockedNumbers = {"5556"};
String incommingNumber;
Context context;
String incomingNumber;
ITelephony telephonyService;
public void onReceive(Context context, Intent intent) {
//Log.v(TAG, "Incoming Call BroadCast received...");
// Log.v(TAG, "Intent: ["+ intent.getAction() +"]");
Bundle b = intent.getExtras();
incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Phone no:"+incomingNumber, Toast.LENGTH_LONG).show();
// Additional Step
// Check whether this number matches with your defined Block List
// If yes, Reject the Call
if(incommingNumber.equals(blockedNumbers[0])){
try {
telephonyService.endCall();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
When i tried to run this example ie make call from one emulator(5556) to the second emulator(5554). It shows Phoneno:null.
I am unable to understand the reason for this.

Block outgoing SMS by contentObserver

I want to block SMS by contentObserver. For that I want to get the phone number of the SMS first. What do I do to get the number? This is the code that I have, just counting the number of SMS.
package com.SMSObserver4;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Contacts;
import android.provider.Contacts.People.Phones;
public class SMSObserver4 extends Activity {
/** Called when the activity is first created. */
private static final String Address = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setReceiver();
}
private SmsSentCounter smsSentObserver = new SmsSentCounter(new Handler());
private int sms_sent_counter = 0;
private void setReceiver() {
this.getContentResolver().registerContentObserver(
Uri.parse("content://sms"), true, smsSentObserver);
}
class SmsSentCounter extends ContentObserver {
public SmsSentCounter(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
#Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
try{
System.out.println ("Calling onChange new");
super.onChange(selfChange);
Cursor sms_sent_cursor = SMSObserver4.this.managedQuery(Uri
.parse("content://sms"), null, "type=?",
new String[] { "2" }, null);
if (sms_sent_cursor != null) {
if (sms_sent_cursor.moveToFirst()) {
sms_sent_counter++;
System.out.println("test" + sms_sent_counter);
}
}
Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Address);
if (phoneUri != null) {
Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
if (phoneCursor.moveToFirst()) {
long person = phoneCursor.getLong(1); // this is the person ID you need
}
}
}catch(Exception e)
{}
}
}
}
I have done a lot of tests and I found this to be impossible. That's because when the messaging application inserts a new record in the SMS content provider, it is already trying to send the SMS. So, even if you detect the SMS in the content://sms/outbox URI, it will be too late to stop it. In fact, there's no way to stop it... it all depends on the SMS application, which you can't interrupt.
Nope, you cant. Observer will comeinto affect after dataset has been modified, by that time sms will already be on the way for delivery. Infact by any means you cant block outgoing sms.

Categories

Resources