Broadcast Receiver not working for voice network - android

I want to receive notification when Mobile network connection is lost or received. Through following code, I can receive notification for Wi-Fi (Data) connection but not for Mobile (Voice) connection.
Manifest :
<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".notifier">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE " />
</intent-filter>
</receiver>
</application>
Java :
public class notifier extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context, "Network Changed !!", Toast.LENGTH_LONG);
toast.show();
}
}
Please tell me, how can I receive notification for Mobile Network (Voice).

Pseudocode!!
PhoneStateListener myListener = new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
// Handle different service states here.
}
};
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
.listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);
http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)

Check the permission in the AndroidManifest.xml, first.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
And try this code below.
ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobile.isConnected()) {
//Do Sth.
}
If this is not work, check your phone's brand. If the phone's developer blocked some codes or not implement some code, it doesn't work. What is your phone?

Related

BroadcastReceiver is not working (detect if wifi is connected)

I'm trying to use a BroadcastReceiver to detect if wifi is connected and simply display a toast when the status changes (wifi disonnect/reconnect/turn off) but it doesn't seem to be working. Any ideas?
ConnectionChangeReceiver class:
public class ConnectionChangeReceiver extends BroadcastReceiver
{
#Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final boolean isWifiConn = networkInfo.isConnected();
Log.d("debug", "Wifi connected: " + isWifiConn);
Toast toast = Toast.makeText(context, "Wifi connected: " + isWifiConn, Toast.LENGTH_LONG);
toast.show();
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<receiver android:name="ConnectionChangeReceiver" android:label="NetworkConnection">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
I go in the app, and disconnect wifi, reconnect, no toast. I disable wifi, no toast. re-enable it, no toast. I suspect that there's something wrong with the broadcast receiver.
Thanks in advance.
I got it working.
My problem was in the Manifest. I was putting my receiver block outside the application tag. Once I moved it inside, it worked perfectly.

Android: How to find out name of the bluetooth device connected?

Basically im trying 2 things here, Im trying to start a toast when my bluetooth device is connected to a particular device (so need to check if that is the particular bluetooth name), if that is the particular device then i want to show a toast when connected to that particular bluetooth device. I also want to show a toast when my bluetooth is disconnected to that particular bluetooth device.
Here's my code:
in manifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>
Class's code:
public class MyBluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVER CALLED!!", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(
"android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
// code for Bluetooth connect
Toast.makeText(context, "CONNECTED!!", Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(
"android.bluetooth.device.action.ACL_DISCONNECTED")){
//code for Bluetooth disconnect;
Toast.makeText(getApplicationContext(),"DISCONNECTED",Toast.LENGTH_LONG).show();
}
}
}
In my code im getting receiver called toast properly and even the toast for disconnected is also working but toast of connected never works.
Please let me know why CONNECTED toast doesn't work and how to make this code work when connected to a particular device ( I don't want to show this toast for all the devices ).
Change your broadcast receiver to:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//you can get name by device.getName()
} else if (BluetoothAdapter.ACL_DISCONNECTED
.equals(action)) {
}
}
};

Invoking an activity from Broadcast Receiver

i am a beginner in android and i am developing an application where BrocastReceiver starts whenever the user makes a call... i am trying to monitor the network status inside the broadcast receiver... and i am trying to start the activity when the user hangs up the phone... but unfortunately it is giving me an error...please let me know how to do this...
My broadcast receiver starts properly when the user make a call... but i do not know how to monitor the network inside the BroadcastReceiver when the user is on call.. and invoke the activity from the BroadcastReceiver when the user hangs up the phone...
Please let me know how to achieve this... Thanks in advance.. :-)
my code...
BroadcastReceiver Code
public class MoniterNetworkStatus extends BroadcastReceiver{
private ConnectivityManager connectivityManager;
private NetworkInfo info;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Monitering Network Status !!!", Toast.LENGTH_LONG).show();
boolean networkStatus = false;
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Intent activityIntent = new Intent(context, NetworkStatusActivity.class);
// I am monitoring the network status as long as user is on call...
while(telephonyManager.getCallState()!= TelephonyManager.CALL_STATE_IDLE){
info = connectivityManager.getActiveNetworkInfo();
if(info == null){
networkStatus = false;
}else if(info.getType() == ConnectivityManager.TYPE_MOBILE){
networkStatus = info.isConnected();
}else{
networkStatus = false;
}
}
//Putting the network details in to the activity.
activityIntent.putExtra("operatorName", telephonyManager.getNetworkOperatorName());
activityIntent.putExtra("networkStatus", networkStatus);
// starting the activity
context.startActivity(activityIntent);
abortBroadcast();
}
}
My Manifest File
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- <uses-permission android:name="android.permission.INTERNET"/> -->
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.abc.activity.NetworkStatusActivity"></activity>
<receiver android:name="com.abc.broadcastreceiver.MoniterNetworkStatus"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.ANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
My broadcast receiver starts properly when the user make a call... but i do not know how to monitor the network when the user is on call.. and invoke the activity when the user hangs up the phone...
Please let me know how to achieve this... Thanks in advance.. :-)
You need to register another receiver to accept ACTION_PHONE_STATE_CHANGED. This may be a duplicate of Detecting outgoing call and call hangup event in android

Auto-accept bluetooth pairing possible?

In the Android 2.3.3 BluetoothChat example with with createInsecureRfcommSocketToServiceRecord() API, users are still prompted to accept the pairing request, even though no PIN code is presented.
Is there a way to automate Bluetooth pairing request without user intervention? Or is this never possible due to security concerns? I have been looking online for 2 days now and haven't really found much, so if anybody knows, please post.
Thanks!
So, I had this cuestion, if some one needs the answer to this working in android 4.4.2
IntentFilter filter = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
/*
* Registering a new BTBroadcast receiver from the Main Activity context
* with pairing request event
*/
registerReceiver(
new PairingRequest(), filter);
and the code for the Receiver
public static class PairingRequest extends BroadcastReceiver {
public PairingRequest() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
//the pin in case you need to accept for an specific pin
Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
//maybe you look for a name or address
Log.d("Bonded", device.getName());
byte[] pinBytes;
pinBytes = (""+pin).getBytes("UTF-8");
device.setPin(pinBytes);
//setPairing confirmation if neeeded
device.setPairingConfirmation(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
and in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
and the broadcastReceiver
<receiver android:name=".MainActivity$PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter>
</receiver>
Not with the standard API, no: if the MAC address is not already in the pairing database there will always be the prompt. I'm told that if you have a device that has been rooted and have public read/write access to the bluetooth service's DBus endpoint you can work around that but I've never seen that actually implemented.
i came across the same problem, i hope the following code will help:
firsly we need:
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter></receiver>
secondly we need the BluetoothDevice class, and:
public class PairingRequest extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
device.setPin(pinBytes);
}
}
}

Listen incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

Is there any way to listen to incoming calls by extending BroadcastReceiver to listen to OS's broadcast,without using PhoneStateIntentReceiver or PhoneStateListener.
Also please tell me what will be action and permissions in manifest.
I've tried with as follows but it is not working for incoming calls but working for outgoing
The only one .java file of app is as follows(the app only one .java file and one manifest file)
package com.crsardar.media.audio;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class IncommingCallReceiverCRS extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Chitta : ", "Its working");
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.crsardar.media.audio"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="IncommingCallReceiverCRS" android:enabled="true">
<intent-filter>
<!--action android:name="android.intent.action.NEW_OUTGOING_CALL"/-->
<action android:name="android.intent.action.ANSWER" >
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
The action you have defined in your manifest is incorrect. this is an Action Intent that can be used to answer a call and not monitor incoming calls.
You can use two broadcast receivers that listen to ACTION_PHONE_STATE_CHANGED and NEW_OUTGOING_CALL broadcast intents.
The ACTION_PHONE_STATE_CHANGED will be received when there is a new incoming call, call answered or hangup (See the documentation for the EXTRAs received with this Intent).
The NEW_OUTGOING_CALL will be received when there is a new outgoing call placed on your device.
As for permissions, I think you got it about right in your manifest (I assume the RECORD_AUDIO permission is used for something else in your application)
Here is My demo for android unit test. You can refer to it.
public interface ICallVerify {
void onOutgoing(Context context, Intent intent);
void onCallStateChange(Context context, Intent intent);
}
protected void setUpCallVerify(final ICallVerify callVerify) { //listen ingoing and outgoing
final CountDownLatch latch = new CountDownLatch(1);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { //state change
Log.i(TAG, "outgoing call...");
callVerify.onOutgoing(context, intent);
} else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){ // state changed
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals("RINGING")) {
state += " number:" + intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
Log.i(TAG, "call state changed.... " + state);
callVerify.onCallStateChange(context, intent);
}
}
};
IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
ContextUtils.getTargetContext().registerReceiver(receiver, filter);
try {
latch.await(5, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Dont forget to add permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>

Categories

Resources