How to get device id in a onRecive function - android

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();
}
}

Related

How to getAssets in a service?

I fail to find out how to getAssets in a service.
for example the receiver:
public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
}
In the AndroidServiceStartOnBoot class, how to get getContext().getAssets().open("server.crt"); ?
Thanks a lot.
Edited:
The question is not how to call getAssets in BroadcastReceiverOnBootComplete class, instead, the question is how to getAssets in the service AndroidServiceStartOnBoot class.
The reason why I posted BroadcastReceiverOnBootComplete class is because that is how I call AndroidServiceStartOnBoot class. Sorry that it's kinda misleading. Thanks.
onReceive gives you the context in your broadcast receiver class
public abstract void onReceive (Context context, Intent intent)
from there, you can use it directly or assign it to your class level variable and you'll be able to use it in other methods as well
Context myContext = null;
#Override
public void onReceive(Context context, Intent intent) {
//here you can access to context directly
context.getAssets().open("MY_FILE");
//or you can save it in your class level variable
myContext = context;
// now you can use this myContext to other methods of this class
}
Edit
Service is an inherent of Context
So in Service, you can directly do like this...
Context context = this;
You already have context
did you try this?
context.getAssets().open("FILE_NAME");
it works for me

Call or text back to a private number

I'm using BroadcastReceiver to determine that calls or messages are from private numbers.
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Bundle onReceiveBundle = intent.getExtras();
String phoneNumber = onReceiveBundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
The phoneNumber returns negative numbers which cannot be called or replied back. Without the need to decode it, is it possible to reply or call back to a private number? If not, how can I decode it?

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"/>

Android Check if call forwarding is activated

I am building a call forwarding application and have used the **21*xxxxxx# ussd code to activate call fowarding using ACTION_CALL Intent. But I have not found a solution to check whether Call forwarding is active or not.
Is there any solution to check from the android system if call forwarding is active or not?
TelephonyManager manager = (TelephonyManager)
this.getSystemService(TELEPHONY_SERVICE);
manager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR );
class MyPhoneStateListener extends PhoneStateListener{
#Override
public void onCallForwardingIndicatorChanged(boolean cfi) {
Log.i(TAG,"onCallForwardingIndicatorChanged CFI ="+cfi);
preferences.edit().putBoolean("CALL_FORWARD_ACTIVE", cfi).commit();
super.onCallForwardingIndicatorChanged(cfi);
}
}
if cfi returns true that means set call forward success
you can make Brodcast class register it and you can track out going call like
public class CallBroadcastReceiver extends BroadcastReceiver
{
public static String numberToCall;
public void onReceive(Context context, Intent intent) {
Log.d("CallRecorder", "CallBroadcastReceiver::onReceive got Intent: " + intent.toString());
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
numberToCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("CallRecorder", "CallBroadcastReceiver intent has EXTRA_PHONE_NUMBER: " + numberToCall);
}
}
}
PRECAUTION:
The following answer is suggestion. I haven't tried it personally So you better try it to check the result.
From your gsm number you can check your call forwarding options by dialing *#21#. So you can try dialing this number from application and read the ussd response.
part 1: to dial the number
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri uri = Uri.fromParts("tel", "*#21#", "#");
intentCallForward.setData(uri);
startActivity(intentCallForward);
part 2: to read the ussd response
There is no API to do this. But here in this SO answer it suggested some methods that you can try.
Best of luck
yes you can use *#21# to check for CF in the same way as you used **21*xxxxxx# to activate CF.
BTW, these are MMI code, not USSD code.

Android SIM change

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);
}
}

Categories

Resources