I've got a following problem:
Why does my CallReceiver works on older android version, while on the newer one nothing happens? Is it any change of permission rules, or what?
There is my code:
public class CallReceiver extends BroadcastReceiver {
static boolean isRinging=false;
static boolean isReceived=false;
static String callerPhoneNumber;
#Override
public void onReceive(Context mContext, Intent intent){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
isRinging =true;
Bundle bundle = intent.getExtras();
callerPhoneNumber= bundle.getString("incoming_number");
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
isReceived=true;
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
// detect missed call
if(isRinging==true && isReceived==false){
Toast.makeText(mContext, "Missed call from : "+callerPhoneNumber, Toast.LENGTH_LONG).show();
}
}
}
}
Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".CallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
Thank you for any help
As per your question, it seems you didn't asked for Run time permissions on higher versions(>=API level 23), so enable them by requesting permissions.
For more check this.
Related
My Broadcast receiver for PHONE_STATE is working for kitkat and Lollipop version even if App is Closed but when I am using Lollipop version its not working when app is Closed
Here is my Manifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BlockCallReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Some new android devices have security application by default. some times these apps lock your auto-start mode can you please check in settings it may be preventing boot broadcast receiver ?
Create a new app and try this;
Broadcast Receiver
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String str = intent.getAction();
if ("android.intent.action.PHONE_STATE".equals(str))
inComing(context, intent);
if ("android.intent.action.NEW_OUTGOING_CALL".equals(str))
outGoing(context, intent);
}
private void inComing(Context context, Intent intent){
String callState = intent.getStringExtra("state");
if ("RINGING".equals(callState)){
Log.i(TAG, "RINGING SENDS BUSY");
}else if ("OFFHOOK".equals(callState)){
Log.i(TAG, "OFFHOOK SENDS BUSY");
}else if("IDLE".equals(callState)){
Log.i(TAG, "IDLE SENDS AVAILABLE");
}
}
private void trueCallerOutgoing(Context context, Intent intent)
{
String callState = intent.getStringExtra("state");
if ("RINGING".equals(callState)){
Log.i(TAG, "RINGING SENDS BUSY");
}else if ("OFFHOOK".equals(callState)){
Log.i(TAG, "OFFHOOK SENDS BUSY");
}else if("IDLE".equals(callState)){
Log.i(TAG, "IDLE SENDS AVAILABLE");
}
}
}
Manifest
<receiver android:name="PhoneStateReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
And Dont forget permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I just want to show a toast when a call is received, But nothing is happening and it doesn't even stop on OnReceive in debugging mode.
I am unable to figure out that why it is not getting stopped at the onReceive. It is just like it is not even receiving an intent .
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rajatgupta.broadcastrecieverexample">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyReciever">
<intent-filter>
<action android:name="rajat_action">
</action>
</intent-filter>
</receiver>
<receiver android:name=".IncomingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
IncomingCall.java
public class IncomingCall extends BroadcastReceiver {
Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
try {
Log.d("Intent", "Intent Detected");
Toast.makeText(context," Receiver start ",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
e.printStackTrace();
}
}
}
What am I doing wrong?
It is no longer enough to just to put the READ_PHONE_STATE permission in the Manifest, you now have to request permissions at runtime.
Permission check:
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_PHONE_STATE);
Request:
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
https://developer.android.com/training/permissions/requesting.html
In my application I am trying to receive broadcast Media_Scanner_Finished. But the receiver is not getting called.
Here is my code-
'<?xml version="1.0" encoding="utf-8"?>'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="objectdistance.ankeshkjaisansaria.ram.sita.MyApp">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="objectdistance.ankeshkjaisansaria.ram.sita.myApp.broadcastreceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_FINISHED"/>
<action android:name="android.intent.action.MEDIA_SCANNER_STARTED"/>
<action android:name="android.intent.action.MEDIA_SCANNER_STARTED"/>
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
</manifest>
In my Broadcast receiver class:-
public class broadcastreceiver extends BroadcastReceiver {
BroadcastReceiver mMediaScannerReceiver;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("INFO", "Enter BroadcastReceiver");
}
}
I think the issue is relating to permission required in manifest file to get access to Media_Scanner broadcast.
One more thing I would like to clear is that :- Does Media_Scanner_Started gets called when content provider Media Image database gets updated ?
Add the receiver in code rather than in the manifest:
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
filter.addDataScheme("file");
scannerStartedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
}
}
I have a PackageBroadcastReceiver running ok while the app is in the background. I need it to keep listening to packages added/removed when it is closed. But, it is not.
Is there any way to do this with a BroadcastReceiver or should I move to a service?
Here the code:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cresan.antivirus.StockingActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.tech.applications.coretools.advertising.PackageBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
And my PackageBroadcastReceiver:
public class PackageBroadcastReceiver extends BroadcastReceiver
{
static IPackageChangesListener _listener;
static public void setPackageBroadcastListener(IPackageChangesListener listener)
{
_listener=listener;
}
#Override
public void onReceive(Context ctx, Intent intent)
{
if(_listener!=null && Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()))
_listener.OnPackageAdded(intent);
if(_listener!=null && Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()))
_listener.OnPackageRemoved(intent);
}
}
Cheers.
You will definitely need to implement a background service to do this. See the developer guide.
You can then programmatically register a BroadcastReceiver in your service:
private PackageBroadcastReceiver packageBroadcastReceiver;
#Override
public void onCreate() {
super.onCreate();
this.packageBroadcastReceiver = new PackageBroadcastReceiver();
this.packageBroadcastReceiver.setPackageBroadcastListener(new IPackageChangesListener() {
public void OnPackageAdded(Intent i) {
//do something here- probably start an activity
}
...
});
IntentFilter packageFilter = new IntentFilter("android.intent.action.PACKAGE_ADDED");
packageFilter.addAction("android.intent.action.PACKAGE_INSTALL");
packageFilter.addDataScheme("package");
this.registerReceiver(this.packageBroadcastReceiver, packageFilter);
}
#Override
public void onDestroy(){
super.onDestroy();
this.unregisterReceiver(this.packageBroadcastReceiver);
}
Try registering broadcast receiver in service. After activity is destroyed, receiver will be destroyed to so you will not get broadcast event.
I have two broadcast receiver. one for sms and one for call.
I confront this error. i see this page:
BroadcastReceiver trying to return result during a non-ordered broadcast - SMS Receiver
but I don't how ti use that suggestion.
Context.sendOrderedBroadcast.
and if this is helpful or not. I have other receiver in this package for sms this receiver work fine. but this one doesn't work.
public class PhoneCallReceiver extends BroadcastReceiver {
Context context = null;
SharedPreferences preferences = null ;
Boolean blacklist;
Boolean contact;
Boolean all;
public void onReceive(Context context, Intent intent) {
preferences = context.getSharedPreferences("modes",Context.MODE_PRIVATE);
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (preferences.getBoolean("all", true)){
Log.i("all", ": " + phoneNumber);
abortBroadcast();
Log.i("block: ", phoneNumber);
}
else if (preferences.getBoolean("blacklist", true)){
boolean str=Search.search(phoneNumber);
if (phoneNumber != null && str ==true) {
abortBroadcast();}}
else if (preferences.getBoolean("c", true)&&getDetails(phoneNumber)){
abortBroadcast();}
else if (preferences.getBoolean("g", true)){
boolean str=SearchInWhiteList.search(phoneNumber);
if (phoneNumber != null && str ==true) {
abortBroadcast();}}
else if (preferences.getBoolean("b", true)){
abortBroadcast();}
else {}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blocker.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FirstPage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsFilter" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="bloker.activity.android.action.broadcast"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<receiver android:name=".PhoneCallReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="bloker.activity.android.action.broadcast"/>
</intent-filter>
</receiver>
<activity
android:name=".Search"
android:label="#string/app_name" >
</activity>
<activity
android:name=".BlockActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SmsFilter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".CustomAdapter"
android:label="#string/app_name" >
</activity>
<activity
android:name=".Setting"
android:label="#string/app_name" >
</activity>
<activity
android:name=".GetAllContact"
android:label="#string/app_name" >
</activity>
<activity
android:name=".WhiteList"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
edit:
broadcastreceiver fro incoming call to android phone is non-ordering. so that cant be aborted using above code. my question is that how can we handle this kind of broadcast?
I know that using this code can block call.
Can I hang up a call programmatically in android?
but my question is that how to use above code to abort call?
You cannot abort the broadcast or programmatically hang up a call. Telephony control can only be done by the Phone app. The broadcasts you are registered to receive are informative only: they tell you about state changes but they are not something which is triggering state changes or driving the telephony state machine.
You can intercept/modify outgoing calls using the ordered broadcast with action of ACTION_NEW_OUTGOING_CALL but there's no public way to intercept an incoming call.