Blaze Tama
Blaze Tama

Reputation: 10948

getBaseContext can't be used , can something replace it?

So I want to use my preference using SharedPreference but I got an issue while using this code :

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context);

I can't use getBaseContext(); method because my Class is not extending Activity Class. What should I do? I tried to use the context that onReceive gave (arg0) but it's still not working.

This is my code :

public class SMSReceiver extends BroadcastReceiver{

private static final int NOTIF_ID = 0;

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

    Bundle bundle = arg1.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null) {
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str += "You Get New SMS from "
                    + msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";

            Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
            **//THIS IS THE SHAREDPREFERENCES**
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(**NEED HELP HERE**);
            boolean notifStatus = sp.getBoolean("notification", true);
            if (notifStatus) {

                NotificationManager nm = (NotificationManager) arg0
                        .getSystemService(Context.NOTIFICATION_SERVICE);

                String tickerText = str;

                Notification notif = new Notification(
                        R.drawable.ic_launcher, tickerText,
                        System.currentTimeMillis());

                notif.flags = Notification.FLAG_AUTO_CANCEL;

                String contentTitle = msgs[i].getOriginatingAddress();
                String contentText = msgs[i].getMessageBody().toString();

                Intent intent = new Intent(arg0, SMSReply.class);
                PendingIntent pi = PendingIntent.getActivity(arg0, 0,
                        intent, 0);

                notif.setLatestEventInfo(arg0, contentTitle, contentText,
                        pi);
                notif.defaults = Notification.DEFAULT_ALL;
                nm.notify(NOTIF_ID, notif);

                String tempSMS = msgs[i].getOriginatingAddress();
                Intent pass = new Intent();
                Bundle bundlePass = new Bundle();

                bundlePass.putString("key", tempSMS);
                pass.putExtras(bundlePass);
            }
        }
    }
}

The idea is I want to turn on/off my notification using a preference

Upvotes: 0

Views: 454

Answers (2)

Blaze Tama
Blaze Tama

Reputation: 10948

ANSWERED : Sorry i made a syntax mistake so basically we can do it like this :

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(arg0);

boolean notifStatus = sp.getBoolean("notification", true);

It's working, thanks :D

Upvotes: 0

a fair player
a fair player

Reputation: 11786

you can do this:

SharedPreferences sp = arg0.getSharedPreferences(yourPreferencesName, arg0.MODE_PRIVATE);

Upvotes: 2

Related Questions