Reputation: 1875
Here below is an example of service, activity and broadcast receiver. An activity is a setting that make changes to a service. The broadcast receiver listens to changes in settings and update service. learner2learner
public class xService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public static void setEnableNotification(int command) {
Log.i("EnableNotification","Updated");
if (command == 0)
enableNotification = true;
...
}
}
The method below is a part of an activity that sends broadcast:
@Override
protected void onCreate(Bundle savedInstanceState) {
....
IntentFilter filter = new IntentFilter();
filter.addAction(NotifyServiceReceiver.ACTION);
registerReceiver(receiver,filter);
}
final String ACTION="broadcast_settings";
public void onClick(View view) {
Intent intent = new Intent(ACTION);
switch (view.getId()) {
case R.id.switchNotification:
if(switchNotification.isChecked()==true)
{
intent.putExtra("EnableNotification", 0);
Log.i("Security365","Notification is enabled.");
}
else if ....
sendBroadcast(intent);
break;
}
The part below is my broadcast receiver:
public class xReceiver extends BroadcastReceiver {
final String ACTION="broadcast_settings";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION)){
int enableNotification = intent.getIntExtra("EnableNotification", 0);
if (enableNotification == 0)
Serurity365Service.setEnableNotification(0);
...
}
}
}
Mainfest
<receiver android:name=".xReceiver" android:enabled="true"></receiver>
Upvotes: 0
Views: 7256
Reputation: 68167
you are sending broadcast in a wrong way. You should send it as below:
public void onClick(View view) {
Intent intent = new Intent("your_action");
switch (view.getId()) {
case R.id.switchNotification:
if(switchNotification.isChecked()==true)
{
intent.putExtra("EnableNotification", 1);
Log.i("Security365","Notification is enabled.");
}
else if ....
sendBroadcast(intent);
break;
}
}
and receive it as:
public class xReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getACtion().equals("your_action"){
int enableNotification = intent
.getIntExtra("EnableNotification", 1);
if (enableNotification == 0)
Serurity365Service.setEnableNotification(0);
...
}
}
}
update you Androidmanifest.xml too:
<receiver android:name=".xReceiver" android:enabled="true">
<intent-filter>
<action android:name="your_action" />
</intent-filter>
</receiver>
Upvotes: 0
Reputation: 49410
If I understand your code correctly, the block
if(switchNotification.isChecked()==true)
{
intent.putExtra("EnableNotification", 1);
Log.i("Security365","Notification is enabled.");
}
is setting the EnableNotification
to 1
in the intent used from sendBroadcast
In your broadcast receiver, you have
int enableNotification = intent
.getIntExtra("EnableNotification", 1);
if (enableNotification == 0)
Serurity365Service.setEnableNotification(0);
So it says to retrieve the extra EnableNotification
and if no value, return the default value of 1
and then you never enter your if (enableNotification == 0)
statement.
To be sure your broadcast receiver works correctly, add a log statement at the beginning of your receiver in the onReceive
method.
EDIT:
Also the AndroidMANIFEST.xml
has a tag <manifest>
that declares a package
.
For example
<manifest package="com.hello.world" ...
When you declare the receiver in the manifest, the .
like in .xReceiver
means that xReceiver.java
should be located in the package com.hello.world
.
If you have in a different package, specify the full name or relatiive to the package
declared in <manifest
More info here
Upvotes: 1