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
Related
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.
I have an activity on which I am initialising a broadcast receiver. I want this broadcast receiver to to call a function in my activity once it has received the message that the network status of the phone has changed.
Unfortunately it seems like the broadcast receiver never receives this message since the onReceive function is never called.
Here the Receiver:
private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (!wifi.isAvailable() || !mobile.isAvailable()) {
Log.d("debug"," in the if");
Handler handler = new Handler();
handler.postDelayed(sendUpdatesToUI, 10);
Log.d("Network Available ", "Flag No 1");
}
}
};
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
private void update() {
Log.d("debug", "got update");
}
I call register it with this line in the onCreate function:
registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
and here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtuya.secmere">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar" />
</application>
</manifest>
EDIT
I have added this in my manifest but still no result:
<receiver
android:name="NetworkReceiver"
android:label="NetworkReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Broadcast receiver also needs to be declared in your manifest.xml
Add
<receiver android:name=".ReceiverName" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
You might want to place receiver in it's own class instead of doing it anonymously.
Don't you have to register receiver in manifest ? Like this:
<receiver
android:name="Receiver"
android:label="Receiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
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 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.
I am testing on an physical device (SAMSUNG ACE GT S5830i)
But I am not receiving the BOOT_COMPLETED intent therefore the service is not Receiver is not starting
This is the code I am using.
public class BootCompleteReceiver extends BroadcastReceiver {
static final String TAG = "InfoService.BroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e(TAG, "System boot notification received.");
Intent service = new Intent(context, InfoService.class);
context.startService(service);
} else {
Log.e(TAG, "Intent received: " + intent);
}
}
}
This is the Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appengine.paranoid_android.lost"
android:versionCode="2"
android:versionName="1.1">
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity android:name=".InfoSetup"
android:label="#string/activity_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LockScreen"
android:label="#string/activity_name"
android:launchMode="singleInstance"
android:clearTaskOnLaunch="true"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<service android:name=".InfoService"
android:label="#string/service_name"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<!-- <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> -->
Try to add the full path with the package for <receiver android:name=".BootCompleteReceiver">
<receiver android:name="com.appengine.paranoid_android.lost.BootCompleteReceiver">