In my application I want to get notified when a call comes in. For this I created a BroadcastReceiver which looks like this:
public void onReceive(Context context, Intent intent)
{
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
if(state == TelephonyManager.CALL_STATE_RINGING) incommingCall(incomingNumber);
else if(state == TelephonyManager.CALL_STATE_IDLE) endingCall(incomingNumber);
}
//...
}
My problem now is that this Receiver is never called. I have registered it in the Manifest like this:
<receiver
android:name="com.cilenco.interrupts.ContactControl"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Is something wrong with this? Or is this a Receiver I can not register from the Manifest? If so do you have other ideas how to implement this without the user has to start my application?
Make you android:exported="false" attribute to true
android:exported="true"
so that android OS can send the broadcasts to your application , if you specify exported to false android OS will ignore yours application.
Try this one:
public class Incomingcallreceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
Do your stuff
}
}
Add this in the manifset to register the receiver for the call
<receiver
android:name="com.example.enwaye_connect.Incomingcallreceiver"
android:label="Incomingcallreceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Add permission also
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_LOGS" />
Related
Broadcast receiver it's work perfectly when I kill my application it work in background. but i
installed my real device it's don't when i kill application.
> Java Code
public class BackgroundService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Send Message",Toast.LENGTH_LONG).show();
}
}
> MainActivity
BackgroundService backgroundService;
#Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(backgroundService,intentFilter);
}
> Manifests
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<receiver android:name=".BackgroundService">
<intent-filter android:priority="999">
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
From android 8 onwards, if you have any implicit broadcast, you have to register it within your activity not in manifest
In your case
<action android:name="android.intent.action.SCREEN_ON"/> <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
seems to be implicit broadcast. Try registering them in your activity class using intentFilter.addAction(Your Action)
Also unregister you receiver in onStop by
calling unregisterReceiver(Receiver instance)
I've a broadcast receiver and it is not getting hot even though the broadcasts re registered in the manifest.
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trial.trialservice"
android:versionCode="2"
android:versionName="BA_1.00.0.0.001"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="21"
/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Material" >
<service
android:name="com.trial.trialservice.TrialService"
android:exported="true"
android:permission="com.encoding.permission.TRIAL_ACCESS" >
</service>
<receiver android:name=".TrialHandler" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.trialmanager.TRIAL_STATUS_UPDATED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the handling part of the code:
public class TrialHandler extends BroadcastReceiver {
private final String TAG = "TrialHandler";
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Inside TrialHandler onReceive, Intent: "+intent);
mContext = context;
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equals("com.trialmanager.TRIAL_STATUS_UPDATED")){
// My business logic goes here
}
}
}
Any help is greatly appreciated. Thanks.
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'm developing an app without GUI. It should start it's service when Wifi button is changed(ON or OFF).
This is my manifest file:
<receiver android:name="com.updater.wifitester.NetWatcher">
<intent-filter
android:priority="100">
<action
android:name="android.net.wifi.WIFI_STATE_CHANGED"
android:enabled="true"
/>
</intent-filter>
</receiver>
Here is the permissions that I gave:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
And I extended Broadcast receiver:
public class NetWatcher extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//check intents and service to know if all is ok then set bWifiStateOk accordingly
Toast.makeText(context, "Wifi on", Toast.LENGTH_LONG).show();
Log.i("wifi_state_changed","wifi changed...");
} else {
return ; // do nothing ... we're not in good intent context doh !
}
}
}
But there is nothing in the log cat and no Toasts shown. What Am I missing?
Please help me guys...
Try this
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>