I want a BroadcastReceiver for incoming calls.
I have searched about this on Stackoverflow and have read the answers but it does not log when Android device starts ringing. Here is my code -
public class Mybrd extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
Log.i("LOG", "LOG ME");
}
}
manifest:
<receiver android:name=".Mybrd">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Check you have register receiver correctly in manifest file.Please register recevier with fully qualified package name if it is not in the root package.
public void onReceive(Context context, Intent intent) {
String phoneState =intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.i("LOG", "LOG ME");}}
Try using the above code. Add permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Related
I'm trying to receive a broadcast when power source connects or disconnects to the device. So I mention the receiver in the manifest file but it doesn't work. But When I declare the broadcast dynamically it works fine.
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "POWER CONNECTION CHANGED", Toast.LENGTH_LONG).show();
}
}
manifest code snippet
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
I added a receiver to listen when app is installed. But it is not working. Here is my code in AndroidManifest.xml
<receiver android:enabled="true"
android:exported="true"
android:name="com.bsp.iqtest.reiceiver.IQTestReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Here is my code in MainActivity (launcher activity) , function onCreate.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IQTestReceiver br = new IQTestReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);
}
Here is my code in IQTestReceiver (this class is written in other file)
public class IQTestReceiver extends BroadcastReceiver {
public IQTestReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String packageName=intent.getData().getEncodedSchemeSpecificPart();
Log.e("HELLO",packageName);
}
}
I set a breakpoint in onReceive function , but it doesn't run when i debug.
Thanks for your helping.
You can not receive PACKAGE_ADDED or PACKAGE_REPLACED for your own app, if that is what you're trying.
"Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast."
See http://developer.android.com/reference/android/content/Intent.html
set your broadcasrt in manifest like this
<receiver
android:name=".IQTestReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="check_values"/>
</intent-filter>
</receiver>
and send the broadcast like this.....Intent it1=new Intent(Intent.ACTION_USER_PRESENT);
it1.setAction("check_values");
it1.putExtra("data_key1",message);
sendBroadcast(it1);
and in on receive would be like this....
#Override
public void onReceive(Context context, Intent intent)
{
data1=intent.getStringExtra("data_key1");
System.out.println("ffffff11" + data1);
}
I just want to know if how to cancel outgoing call in android dev.? Is it possible in the simplest way of coding?
You need to create a BroadCastReceiver that will be called on the action : NEW_OUTGOING_CALL
Java Class :
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
setResultData(null);
abortBroadcast(); // cancel the call
}
}
}
Manifest declaration
Edit
don't forget add out going permission
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<receiver android:name=".CallReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
With this code, any call will be canceled and you will not be able to do a call if this app is on your phone
1.Create a BroadcastReceiver with a priority of 0.
<receiver android:name=".YourReceiver" >
<intent-filter android:priority="0" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
2.In the BroadcastReceiver intercept the ACTION_NEW_OUTGOING_CALL intent in its onReceive method
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
setResultData(null);
}
}
3.call setResultData(null) in the onReceive as shown in above code/method
Add below permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
So I have a boot receiver that is supposed to call an intent service but the receiver isn't registering at all.
Manifest file -
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".ClockReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
ClockReceiver.java
public class ClockReceiver extends BroadcastReceiver {
private final String TAG = "ClockReciever";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"onRecive");
context.startService(new Intent(context, RefreshIntentService.class));
}
}
I think this is correct, but according to my logcat the ClockReciever is never called and the program crashes with a "Unable to instantiate receiver" error.
Any suggestions? Thank you
Here you have a typo
<receiver android:name=".ClockReciever">
Should be ClockReceiver, i.e. same as your class.
Cheers!
I have a broadcast receiver defined in the manifest with the android.bluetooth.device.action.ACL_CONNECTED in the intent filter.
It's triggered fine when the app is in the stack, but after I stop it from android settings it won't trigger anymore. any suggestions?
Update for Menny:
<receiver android:name=".auto.AppLauncher">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
</intent-filter>
</receiver>
Did you program a BroadcastReceiver in your Code?
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
//something you want to do here
}
}
}