I am trying to code APK to identify the received calls are in sim 1 or sim 2. I have tried below solutions but in all devices, it is not working. Samsung and MI devices the below solution is not working. can you please suggest universal solutions. thanks
I tried below solutions
URL1
URL2
public class IncomingCallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String callingSIM = "";
Bundle bundle = intent.getExtras();
callingSIM =String.valueOf(bundle.getInt("simId", -1));
if(callingSIM == "0"){
// Incoming call from SIM1
}
else if(callingSIM =="1"){
// Incoming call from SIM2
}
}
}
Related
I have a class that extends BroadcastReceiver and reads code from an SMS and pastes it automatically in an EditText , this works perfectly on Samsung and Nexus Emulator, but now it's not working on Huawei , it does copy the text.. but then user needs to paste it manually..
This is my code:
public class SimpleSmsReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) Objects.requireNonNull(pudsBundle).get("pdus");
String format = pudsBundle.getString("format");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) Objects.requireNonNull(pdus)[0],format);
// check if sender is specificSender
if(messages.getOriginatingAddress().equalsIgnoreCase("specificSender")) {
// get only the numbers from the sms
String number = messages.getMessageBody().replaceAll("[^0-9]", "");
// place the code in the edit text
if(VerifyActivity.isVerifyRunning) {
VerifyActivity.et_code.setText(number);
} else if(BaseActivity.isChangeNumberOn)
{
EditText et = BaseActivity.changeNumber.findViewById(R.id.et_code);
et.setText(number);
}
}
}
}
Do you have any idea what might cause this?
is it related to Huawei devices or is there something I should add to my
code?
Thank You.
I am developing an Android application which involves showing the user a list of nearby Bluetooth devices and connecting to the device selected by them. I'm trying to use the system bluetooth device picker as shown in these posts:
How to retrieve Bluetooth device info with Android Bluetooth device picker?
Android Bluetooth Device Picker Usage
The device picker does show, but I can't find out which device was selected in my code. The toast inside the onReceive does not show, which suggests that no broadcast is being received.
Another problem I faced is that if I try to start the device picker activity inside the onRequestPermissionsResult, the device picker does not show up at all, despite clicking 'allow' in the request permission dialog. The toast inside doesn't get displayed either.
Here's the code:
//Code inside Fragment
BroadcastReceiver mReceiver;
BluetoothAdapter bluetoothAdapter;
BluetoothSocket bsock;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.controller_mode_layout,container,false);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//Get location access permission.
if (bluetoothAdapter != null) {
if (bluetoothAdapter.isEnabled()) {
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, reqCode);
}
}
//Receiver to get the selected device information
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context,"Device"+device.getAddress(), Toast.LENGTH_SHORT).show();
try {
bsock=device.createRfcommSocketToServiceRecord(UUID.fromString("00002415-0000-1000-8000-00805F9B34FB"));
bsock.connect();
//Send and receive data logic follows
} catch (IOException e) {
e.printStackTrace();
}
}
};
getActivity().registerReceiver(mReceiver, new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED"));
showDevicePicker();
return myView;
}
#Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
{
Toast.makeText(getActivity(),"Permission result", Toast.LENGTH_SHORT).show();
if((requestCode == reqCode) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
{
//Not working
// showDevicePicker();
}
}
public void showDevicePicker()
{
//Launch built in bluetooth device picker activity
startActivity( new Intent("android.bluetooth.devicepicker.action.LAUNCH")
.putExtra("android.bluetooth.devicepicker.extra.NEED_AUTH", false)
.putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 0)
.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.example.ankit2.controllerapp1")
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.example.ankit2.controllerapp1.Fragment1")
.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
}
Any help will be appreciated.
Note: I tested the code on a Lenovo K3 note running Marshmallow.
The problem was in the DEVICE_PICKER_LAUNCH_CLASS, which was specified as Fragment1. The launch class must be an activity, even if the broadcast receiver is inside a fragment. In this case, changing the launch class to ControllerActivity (which contains the fragment Fragment1) fixed the problem.
I have tried to get device activity such as device is traveling in Vehicle, bicycle, walking or running or not travelling using google play service library but ActivityRecognitionResult getting as null.
Tried out code is in Class1.java:-
public class Class1 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
public static String ACTION_STRING= "action";
Intent intentp = new Intent(context, Class2.class);
intentp.setAction(ACTION_STRING);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentp, PendingIntent.FLAG_ONE_SHOT);
}
}
And code in Class2.java:-
public class Class2 extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() == Alarm.ACTION_STRING)
{
if(ActivityRecognitionResult.hasResult(intent)) //Here condition getting as false
{
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = result.getMostProbableActivity();
int activityType = detectedActivity.getType();
if(activityType != DetectedActivity.STILL)
{
//Here doing something;
}
}
}
}
}
How to detect activity of device using Google Play Services?
From the documentation:
Use of the ActivityRecognitionClient requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.
It the permission properly requested by your app?
I would like to know how can I check when mobile data is connected using BroadcastReceiver.
Here's what I have so far:
private BroadcastReceiver MobileDataStateChangedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(TelephonyManager.EXTRA_STATE,
TelephonyManager.DATA_DISCONNECTED);
if (state == TelephonyManager.DATA_CONNECTED) {
mobileStatus.setText("Connected");
} else if (state == TelephonyManager.DATA_DISCONNECTED) {
mobileStatus.setText("Disconnected");
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status_page);
mobileStatus = (TextView) this.findViewById(R.id.textView4);
registerReceiver(MobileDataStateChangedReceiver, new IntentFilter(
TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
What am I doing wrong in here?
I used the same concept on Wi-Fi checking and it worked great?
I am using the permissions of:
<uses-permission android:name="android.permission.INTERNET"/>
The TelephonyManager.ACTION_PHONE_STATE_CHANGED does not seem to work because as I could read in TelephonyManager class description it only fires with calls, but not with networks or mobile data.
Instead of this, I suggest you try setting a listener. It would be something like this:
1) Get the service with TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TELEPHONY_SERVICE)
2) Set a listener with tm.listen(myCustomPhoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
3) Once you have the listener you will be able to send custom intents to your BroadcastReceiver, if you still want to do that.
I need to locate my current location in a building by scanning the strongest available Wifi to launch an activity. I want to locate only 3 places in that building. So if I am currently near any of those locations, I will click a button to scan the strongest available Wifi to launch an activity.
I need to scan available Wifi in a building.
Get the 3 strongest signal.
Then detect whether any of those 3 strongest signal's SSID is the same with the specific SSID that I want to locate.
If my SSID is one of the strongest 3 signals, then the application will launch a xml layout activity,
So far I already have the coding to scan strongest available Wifi. But I don't know how to launch an activity when the strongest specific Wifi SSID detected. I do not need the information about the Wifi, just to launch my activity layout that has my information.
At the moment I'm using the following bit of code to scan the strongest Wifi signal that I got from here:
WiFiDemo.java
public class WiFiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
Button buttonScan;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
#Override
public void onStop() {
unregisterReceiver(receiver);
}
public void onClick(View view) {
Toast.makeText(this, "On Click Clicked. Toast to that!!!",
Toast.LENGTH_LONG).show();
if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() wifi.startScan()");
wifi.startScan();
}
} }
WiFiScanReceiver.java
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFiDemo wifiDemo;
public WiFiScanReceiver(WiFiDemo wifiDemo) {
super();
this.wifiDemo = wifiDemo;
}
#Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifiDemo.wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
Toast.makeText(wifiDemo, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() message: " + message);
}
}
You just need to start your activity from onReceive. Unless I'm not understanding your question, you just need to fire off your activity.
Since you have the context on your receiver's onReceive you just need to startActivity()
Start Activity inside onReceive BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
Don't forget to define a proper filter for it in your manifest.
http://developer.android.com/reference/android/content/BroadcastReceiver.html