从另一个类访问SharedPreferences(非活动)

时间:2015-04-09 10:43:01

标签: android sms android-service sharedpreferences

我正在尝试使用SharedPreferences将MainActivity类中的值传递到SMSReceiver类,但返回的值不是真的

我使用thisthis,但似乎无效

编辑:我正在我的程序中使用服务,因此我无法调用MainActivity.MyPREFENRECES,SMSreceiver.rK等成员,因为在我杀死活动后我得到null异常

编辑我正在调用布局中运行的方法

任何帮助吗??

MainActivity.java

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;


public class MainActivity extends Activity {

   public static final String MyPREFERENCES = "MyPrefs" ;
   public static Switch smsSwitch;
   private EditText ringKey,vibrateKey,silentKey;
   private AudioManager myAudioManager;
   SharedPreferences sharedpreferences;


    @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);


      smsSwitch = (Switch)findViewById(R.id.smsSwitch);

      ringKey = (EditText)findViewById(R.id.ringWord);
      vibrateKey = (EditText)findViewById(R.id.vibrateWord);
      silentKey = (EditText)findViewById(R.id.silentWord);

      myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      smsSwitch.setChecked(true);

        if (sharedpreferences.contains(SMSreceiver.rK))
        {
            ringKey.setText(sharedpreferences.getString(SMSreceiver.rK, ""));

        }
        if (sharedpreferences.contains(SMSreceiver.sK))
        {
            silentKey.setText(sharedpreferences.getString(SMSreceiver.sK, ""));

        }
        if (sharedpreferences.contains(SMSreceiver.vK))
        {
            vibrateKey.setText(sharedpreferences.getString(SMSreceiver.vK, ""));

        }
        smsSwitch.setChecked(sharedpreferences.getBoolean("checked",false));


   }

   public void vibrate(View view){
       myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
   }
   public void ring(View view){
      myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
   }
   public void silent(View view){
      myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
   }
   public void mode(View view){

     int mod = myAudioManager.getRingerMode();

     if(mod == AudioManager.RINGER_MODE_NORMAL){
         Toast.makeText(getApplicationContext(), "Normal Mode", Toast.LENGTH_SHORT).show();
     }
   else if(mod == AudioManager.RINGER_MODE_SILENT){
         Toast.makeText(getApplicationContext(), "Silent Mode", Toast.LENGTH_SHORT).show();
     }
   else if(mod == AudioManager.RINGER_MODE_VIBRATE){
         Toast.makeText(getApplicationContext(), "Vibrate Mode", Toast.LENGTH_SHORT).show();
     }
   else{

   }
  }
    public void run(View view){
        String r  = ringKey.getText().toString();
        String v  = vibrateKey.getText().toString();
        String s  = silentKey.getText().toString();

        boolean c = smsSwitch.isChecked();

        Editor editor = sharedpreferences.edit();

        editor.putString("ring", r);
        editor.putString("vibrate", v);
        editor.putString("silent", s);

        editor.putBoolean("checked",c);

        editor.commit();

    }

}

SMSreceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;


public class SMSreceiver extends BroadcastReceiver
{
    private final String TAG = this.getClass().getSimpleName();
    public static final String SMS_BUNDLE = "pdus";
    public static SharedPreferences sharedpreference;

    public static String rK="ring";
    public static String vK="vibrate";
    public static String sK="silent";


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

        Intent startServiceIntent = new Intent(context, MainActivity.class);
        context.startService(startServiceIntent);

        sharedpreference = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();

                AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

                if(sharedpreference.getBoolean("checked",true)) {

                    if (smsBody.contains(sharedpreference.getString(rK,"ring"))) {
                        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        Toast.makeText(context, "Normal Mode Activated", Toast.LENGTH_SHORT).show();

                    } else if (smsBody.contains(sharedpreference.getString(vK,"vibrate"))) {
                        am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                        Toast.makeText(context, "Vibrate Mode Activated", Toast.LENGTH_SHORT).show();

                    } else if (smsBody.contains(sharedpreference.getString(sK,"silent"))) {
                        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                        Toast.makeText(context, "Silent Mode Activated", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

    }

}

0 个答案:

没有答案