1

How can I get the method in my BroadcastReceiver into my Fragment? Or is it even possible? receiver:

public class Receiver extends BroadcastReceiver {


    public void test(Context c) {
     ....
    }

}

Fragment:

public class Test extends Fragment {
...
}

So a error appeared:

Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at

com.test.tab.MyReceiver.sendNotificationIfTimeEnd01(MyReceiver.java:47)

        at com.test.tab.Juli.broadcastIntent(Juli.java:54)

        at com.test.tab.Juli.onCreateView(Juli.java:188)

The code for those lines are: in Receiver:

    public void sendNotificationIfTimeEnd01(Context c) {
        Context context = c.getApplicationContext();
        Intent intent01 = new Intent(context, MainActivity.class);
....
}

and in Fragment:

    receiver.sendNotificationIfTimeEnd01(context);
    broadcastIntent();

EDIT UPGRADED CODE: Fragment:

public class FragmentTest extends Fragment {

    private Context context;


    IntentFilter filter = new IntentFilter("com.example.Broadcast");
MyReceiver receiver = new MyReceiver();

// Call this method when the condition is met.
public void broadcastIntent() {
    Intent intent = new Intent();
    intent.setAction("com.example.Broadcast");
    getActivity().sendBroadcast(intent);
}

...

    if (diffDays <= 0 && diffHours <= 0 && diffMinutes <= 0) {
        ((TextView) android.findViewById(R.id.test)).setText("test works");
        if (!notification043 && !buttonColor01.equals("red01")) {
            broadcastIntent();
            editor.putBoolean("notification43", true);
            editor.apply();
        }
    } 

}

MyReceiver:

public class MyReceiver extends BroadcastReceiver {
    public static final int NOTIFICATION_ID_01 = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        sendNotificationIfTimeEnd01(context);
    }

    public void sendNotificationIfTimeEnd01(Context c) {
        Context context = c.getApplicationContext();
        Intent intent01 = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent01 = PendingIntent.getActivity(context, 1, intent01, 0);
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_stat_notification)
                        .setContentIntent(pendingIntent01)
                        .setAutoCancel(true)
                        .setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher))
                        .setContentTitle(testArray[0])
                        .setContentText("testready")
                        .setSubText("click here");
        NotificationManager notificationManager =
                (NotificationManager) c.getSystemService(c.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID_01, builder.build());
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(c.getApplicationContext(), notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 Answers1

2

Actually, you could use Intents :

public class Test extends Fragment {
    ...
    // Call this method when the condition is met.
    public void broadcastIntent() {
        Intent intent = new Intent();
        intent.setAction("com.example.Broadcast");
        getActivity().sendBroadcast(intent);
    }
}

And declare that your broadcast receiver can react to this kind of Intent either through the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.BroadcastDetector"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.example.Broadcast" >
                    </action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

or programmatically

IntentFilter filter = new IntentFilter("com.example.Broadcast");

MyReceiver receiver = new MyReceiver();
getActivity().registerReceiver(receiver, filter);

Then you can intercept this Intent in the receiver :

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

    }
}
Francis Toth
  • 1,595
  • 1
  • 11
  • 23
  • do you mean with registerReceiver the method inside my Broadcast receiver?I cannot resolve it. – Alex mc clair Sep 06 '15 at 13:57
  • You can look at this : http://stackoverflow.com/questions/18842517/broadcast-receiver-class-and-registerreceiver-method But basically, registerReceiver is a method of the Activity class, which allows to register a broadcastReceiver programmatically. I've updated my answer to reflect that. In the case of a Fragment, you have to refer to its parent activity in order to use it. – Francis Toth Sep 06 '15 at 14:01
  • ahh so I can choose between writing it into the manifest or doing it programmatically – Alex mc clair Sep 06 '15 at 14:03
  • You could even use a local broadcast receiver. It may suits better to your needs : http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – Francis Toth Sep 06 '15 at 14:05
  • First I will try to make this work:) But thank you very much – Alex mc clair Sep 06 '15 at 14:07
  • Hi again console told me: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference I will edit the question for further information – Alex mc clair Sep 06 '15 at 14:16
  • Actually, the code that creates the Intent should be in the Fragment, not in the receiver (look at the first lines of my answer). Once the Intent is broadcasted, it will be caught by the receiver in the onReceive method. Is this clearer ? – Francis Toth Sep 06 '15 at 14:38
  • What do you mean ? The 'com.example.Broadcast' is the name of the Intent that can be received by the receiver. It can be anything. I highly suggest you to check on the documentation (http://developer.android.com/reference/android/content/BroadcastReceiver.html), it's very well written, and there'a topic about what you've just mentioned : http://developer.android.com/guide/components/intents-filters.html – Francis Toth Sep 06 '15 at 15:10
  • My English sucks when I am writing fast sorry. forget the question=) – Alex mc clair Sep 06 '15 at 15:38
  • OK I will now edit my question and show you what I did. – Alex mc clair Sep 06 '15 at 15:42
  • So console shows no error or anything but I do not get a notification. No clue right now – Alex mc clair Sep 06 '15 at 15:52
  • What have you put inside the onReceive method ? Could you put your example on github ? – Francis Toth Sep 06 '15 at 16:09
  • this:sendNotificationIfTimeEnd01(context); – Alex mc clair Sep 06 '15 at 16:20
  • I don*t know maybe if you have time we could have a chat. – Alex mc clair Sep 06 '15 at 16:21
  • https://chat.stackoverflow.com/rooms/88935/android-get-method-of-broadcastreceiver – Francis Toth Sep 06 '15 at 16:21