Android SIM change - android

Is it possible to detect SIM number using TelephonyManager in android at boot startup ,using Service at bootup...
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String ss=tm.getSimSerialNumber();

You need to register a broadcast receiver for the boot completion action i.e android.intent.action.BOOT_COMPLETED
in onReceive of this receiver you can start your service get SIM number with below code lines
TelephonyManager telephoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = telephoneMgr.getLine1Number();
Also need to have permission for reading phone number as READ_PHONE_STATE in manifest file.
you can start service from broadcast receiver as -
public class BootListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context,Myservice.class);
context.startService(intent);
}
}

Related

Android : Run service when my app is not in use

In my application I'am using BroadcastReceiver to track telephone state.
There are 3 states available in TelephonyManager.
EXTRA_STATE_IDLE
EXTRA_STATE_OFFHOOK
EXTRA_STATE_RINGING
In my scenario I want to show simple ui which shows some details about the caller while the phone is ringing and i want to close that ui when the call attended by user or call ended.
I'am calling my service when the phone state is Ringing. If my app is in use then I can see my ui part while the phone is ringing. If my app is not in use then I cant see the ui part.
I have checked in my background. The service is not running. I think my service is running only when my app is in use and it is not running when my app is not in use.
To overcome this what I should do?
This is my code.
#Override
public void onReceive(Context context, Intent intent) {
LoginActivity.login.finish();
MainActivity.main.finish();
telephonyRegister(context,intent);
}
public void telephonyRegister(final Context context, Intent intent)
{
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imeiSIM1= telephony.getDeviceId();
System.out.println("IMEI NUMBER"+imeiSIM1);
ctx=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK))
{
try{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)){
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
isringing=true;
Intent intent11 = new Intent(ctx,ProjectDailogActivity.class);
intent11.putExtra("incoming_number",phoneNumber);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent11);
}
}
Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
I got the window like truecaller.
Just the values which i mentioned are default one.
But the same window is not coming when my app is not in use.
Please give me an idea.
that is why android developers invented the BroadcastReciever - it allows you to hook your code as a service in response for system events (such as boot, airplane mode, etc)
Vogella covers it very well: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
and for your situation (send commands as a response for the phone state) you can also use this article which provide a fully working example: http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86
article hightlights:
<!-- Manifest.xml -->
<receiver android:name=".IncomingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
and in java - add these classes
//your Listener class:
private class MyPhoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
// state = 1 means when phone is ringing
if (state == 1)
{
// do something
}
}
}
//Reciever class:
public class IncomingCall extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
No need to run it in a service just in the xml of your application in the reciever section use with phone ringing action and in your recieve implement your UI. and have a different reciever for your app to listen to when the phone is cut.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e("onStartCommand", "onStartCommand");
return Service.START_STICKY;
}
http://developer.android.com/reference/android/app/Service.html
Use AlarmManager to activate service after a specific time and stop service and also kill AlarmManager when call came.
try like this,
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
it will repeating while you manually not stopped it...may this helps you...

Get mobile no or sim serial no in a call

when we receive a call we can get incoming mobile no by below mentioned code,but If it is a dual sim mobile How do I get my ringing number or sim serial number ?Please provide me any solution for this matter.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
strPhoneNumber =intent.getExtras().getString("incoming_number");
}
}
TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String simLineNumber = telMgr.getLine1Number();
obviously, you'll need to ask for:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Call Receiver makes my app "force close"

it all works fint till i answer the phone call,then a few seconds later and i get a "force close" message...any idea what is wrong?
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener imPhoneListener = new PhoneStateListener();
telephony.listen(imPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Intent intent1=new Intent("android.intent.action.Checker");
intent1.putExtra("phoneNr", phoneNr);
context.startActivity(intent1);
}
oh and by the way, how can i promise that my call reciver always work first even if the app is in the background?
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener imPhoneListener = new PhoneStateListener();
telephony.listen(imPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Without seeing the logcat it's hard to say for certain but you shouldn't be doing the above.
TelephonyManager is a system service and calling listen(...) is registering a permanent reference to your imPhoneListener object.
A BroadcastReceiver exists only as long as the code in onReceive() is being executed. This means once onReceive() exits, your imPhoneListener is no longer a valid object but TelephonyManager still holds a reference to it.
When you answer the phone call, you change the phone 'state' and the TelephonyManager will attempt to notify the non-existent imPhoneListener object. Something has to break here.
Get rid of those three lines as they effectively do nothing of any purpose for your BroadcastReceiver anyway - I'm not even sure what you're trying to acheive there.

android is there Bluetooth receiver like sms?

hello everyone i face a real problem i have an arduino device that sends text information via bluetooth and i want to receive this text on my android phone but i want to check the ((text))before any reaction
is there any way to do that like SMS ((BroadcastReceiver))
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
I don't know how exactly to do that. But in the same way you use Receiver for SMS you can use Receiver for Bluetooth.
Register receiver (Manifest or in code)
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
And define the receiver
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name;
name = device.getName();
}
}
};
I suppose : with the communication you can catch a different pram in intent that gives to you the text.
if you want some examples take a look.
http://android-er.blogspot.com.es/2011/05/scan-bluetooth-devices.html AND
http://luugiathuy.com/2011/02/android-java-bluetooth/

How to get device id in a onRecive function

im trying to get the device id inside a onRecive (which is inside broadcastreceiver) but the only way i found is to use this code:
TelephonyManager tManager = (TelephonyManager)myActivity.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
device_id = tManager.getDeviceId();
but here i am not in an Activity. so how can i get the device id?
Thanks!
If you are on onReceive then you have a Context.
If you have a context then you can call getSystemService and do the same thing as here
use this:
public class PhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
device_id = tManager.getDeviceId();
}
}

Categories

Resources