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>
Related
I want to detect the outgoing call when it is answered, I use a BroadcastReciever with the following code:
class OutgoingReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string number = intent.GetStringExtra(Intent.ExtraPhoneNumber);
string stateStr = intent.Extras.GetString(TelephonyManager.ExtraState);
if (stateStr!=null)
{
if (stateStr.Equals(TelephonyManager.ExtraStateOffhook))
{ }
else if (stateStr.Equals(TelephonyManager.ExtraStateIdle))
{ }
}
}
}
and use with this code the following permissions:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE" />
And also with this receiver on AndroidManifest.xml
<receiver android:name="OutgoingBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE_CHANGED" />
</intent-filter>
</receiver>
But it is fired only when the outgoing call is start or end.
Please, Help me!!
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.
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" />
I am developing a small android application in which I wanted to start some service once the device boot is completed.
This is the code I used to try to accomplish this, but it not receiving boot complete event, so it isn't working.
//in manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
//my schedule receiver
public class MyScheduleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
Log.i("*************************TEST", "Service loaded at start");
Toast.makeText(context, "device started", Toast.LENGTH_SHORT).show();
}
}
}
MyScheduleReceiver never gets triggered; am I doing something wrong?
You miss a dot before MyScheduleReceiver
<receiver android:name=".MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>