I register a BroadcastReceiver within MyService,
public class MyService extends Service {
final static String ACTION_ONE = "one";
final static String ACTION_TWO = "two";
BroadcastReceiver receiver;
and a inner class
// use this as an inner class like here or as a top-level class
public class MyReceiver2 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do something
Log.d("tag", "received! MyReceiver2");
}
// constructor
public MyReceiver2(){
}
}
onCreate method:
#Override
public void onCreate() {
receiver = new MyReceiver2();
IntentFilter filter = new IntentFilter();
filter.addAction(MyService.ACTION_ONE);
filter.addAction(MyService.ACTION_TWO);
MyService.this.registerReceiver(receiver, filter);
}
And the code below is in MainActivity to send a broadcast:
sendBroadcast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MyService.class);
intent.setAction(MyService.ACTION_TWO);
sendBroadcast(intent);
}
});
I can't understand why MyService(started, and is not yet destoryed) can not receive broadcast.
If I change the receiver variable to a MyReceiver class that extends BroadcastReceiver and has already been proved to work,
MyService is still not receiving broadcast.
myReceiver = new MyReceiver();
Here is MyReceiver(it is proved that it works well):
public class MyReceiver extends BroadcastReceiver{
public MyReceiver(){
Log.d("tag", "MyReceiver Constructor");
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d("tag", "received");
}
}
============EDIT================
manifest
Both
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="one" />
<action android:name="two" />
</intent-filter>
</service>
and
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
do not work.
This looks like a manifest problem,
Please make sure you are declaring that receiver in the manifest!
for example:
<receiver android:name=".receivers.BackgroundTasksReceiver"/>
Related
This is my class where am getting Notification from server->
public class GCMListener_Service extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
Intent notificationReceived = new Intent("com.example.mypackage”);
notificationReceived.putExtra(“key", fromWhere);
sendBroadcast(notificationReceived);
}
}
this i have declared in AndroidManifiestfile:
<service
android:name="com.example.pushnotification.GCMListener_Service"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
here i am trying to get Notification :
public class PushMessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Timber.d("Received push notification”);
}
}
here is my receiver in AndroidManifiest file->
<receiver
android:name=".notification.PushMessageReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.mobile.pushnotification.notificationReceived" />
</intent-filter>
</receiver>
using given code below android 8 (oreo ) it working fine i am getting and receive notification message in PushMessageReceiver class in onReceive method but in android i am unable to get notification unable to send broadcast can any one please suggest me how to send broad cast in Android O .
In your Application class register and unRegister class like this:
public class YourProjectApplication extends MultiDexApplication {
private static YourProjectApplication sInstance;
public static YourProjectApplication getInstance() {
return sInstance;
}
#Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public void registerInternetConnectivityBroadCast(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(appInternetStatus, intentFilter);
}
public void unSetConnectivityListener(AppInternetStatus.InternetConnectivityReceiverListener listener) {
try {
unregisterReceiver(appInternetStatus);
}catch (Exception e){
e.printStackTrace();
}
try {
AppInternetStatus.internetConnectivityReceiverListener = listener;
}catch (Exception e){
e.printStackTrace();
}
}
And then in your mainActivity class (which can also be your base class) call your broadcast receiver like this:
getInstance().registerBroadCast();
getInstance().setConnectivityListener(this);
And in order to unregister your notification. In your onPause() method of your main activity class :
#Override
protected void onPause() {
super.onPause();
getInstance().unSetConnectivityListener(null);
}
I tried to register the broadcast receiver in the manifest but the onReceive method isn't invoked. What am I doing wrong?
manifest.xml
<receiver
android:name="zzz.yyy.xxx.ui.storyboard.discussion.DiscussionFragment$MessageCountReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT" />
</intent-filter>
</receiver>
Sending broadcast as below:
Intent intent = new Intent("zzz.yyy.xxx.ui.storyboard.discussion.GROUPCHAT");
intent.putExtra(BundleKeys.notification_post_id_forum,postId);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Receiving broadcast in Fragment class as below:
#Override
public void onPause() {
clearUnreadCount();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(messageRefresher);
super.onPause();
}
public class MessageCountReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String noti_post_id = intent.getStringExtra(BundleKeys.notification_post_id_forum);
int tempCount=PreferencesIMDStar.getIntValue(getActivity(),noti_post_id);
tempCount++;
PreferencesIMDStar.setIntValue(getActivity(),noti_post_id,tempCount);
if (getActivity() != null && noti_post_id.equalsIgnoreCase(post_id)) {
discussionPresenter.fetchCommentReply(post_id, true);
}
}
}
I know that exists thread about that, but I can't get how to do it, I only need send data a Broadcast, this is the code
Main activity
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}
BroadCast
public class Notification_Reciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
String value=intent.getStringExtra("KEY");
if(Constantes.ACTION.PLAY_ACTION.equals(action)){
actionPlay();
Toast.makeText(context,""+value,Toast.LENGTH_SHORT).show();
}
if(Constantes.ACTION.PREV_ACTION.equals(action)){
}
if(Constantes.ACTION.NEXT_ACTION.equals(action)){
}
}
Manifest
<receiver android:name=".Notification_Reciever">
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
</receiver>
Always get null
Since you have intent-filter defined in manifest,
<intent-filter>
<action android:name="PLAY_ACTION"/>
</intent-filter>
You need to set action to your intent in order to receive data...
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION");
path = String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath());
passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
You have no set an action to the intent which you are broadcasting.
passIntent = new Intent("action");
In your case 'passIntent = new Intent(Constantes.ACTION.PREV_ACTION);` or whichever the action that you want to broadcast.
You can also use intent.setAction(action) to set an action.
you need to set action here.In manifest it is already registered
public void passData(){
passIntent = new Intent(this,Notification_Reciever.class);
passIntent.setAction("PLAY_ACTION"); path=String.valueOf(recibidor_file.get(recibidor_position).getAbsolutePath();passIntent.putExtra("KEY",path);
sendBroadcast(passIntent);
}
Here is an example of broadcast. do it like this
Intent intent = new Intent("IncomingChat");
intent.putExtra("visitor", String.valueOf(eventParameters[0]));
intent.putExtra("Action", "receivedincommingchats");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
and add this to the activity where you want to receive the broadcast
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(IncomingBroadcastReceiver, new IntentFilter("IncomingChat"));
}
private final BroadcastReceiver IncomingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String Action = intent.getStringExtra("Action");
switch (Action){
case "receivedincommingchats":
String recVisit = intent.getStringExtra("visitor");
break;
default:
break;
}
}
};
Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("data", "data");
sendBroadcast(intent);
your manifest file should have something like this ..
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.example.Broadcast" >
</action>
</intent-filter>
</receiver>
You have to register your broadcast receiver in your activity..
IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);
And your broadcast receiver will be like this
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Implement code here to be performed when
// broadcast is detected
}
}
I want my app to be aware anytime the user changes the locale. So in my Application class, I create a receiver to listen to the system intent ACTION_LOCALE_CHANGED:
public final class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale, Toast.LENGTH_LONG).show();
}
};
#Override public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
}
}
When I press home and go to the settings app to change my locale, the Toast is not shown. Setting the breakpoint inside onReceive shows it never gets hit.
Why do you want the BroadcastReceiver in Application class. My suggestion is to have a separate class for BroadcastRecevier.
public class LocaleChangedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction (). compareTo (Intent.ACTION_LOCALE_CHANGED) == 0)
{
Log.v("LocaleChangedRecevier", "received ACTION_LOCALE_CHANGED");
}
}
}
and register your Brodcast receiver in Manifest file.
<receiver
android:name=".LocaleChangedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>
Intent.ACTION_LOCALE_CHANGED is not a local broadcast, so it won't work when you register it with LocalBroadcastManager. LocalBroadcastManager is used for the broadcast used inside your app.
public class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale,
Toast.LENGTH_LONG).show();
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
}
How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>