1

我是 android 编程的新手,我遇到了关于广播接收器中的 onReceive() 方法的问题。我想从另一个类执行一个特定的方法,即 MainActivity.java,它具有 TurnGPSOnOff() 的方法。就像这样,每次系统接收到与共享首选项中存储的值相等的 SMS 时,如果 GPS 被禁用,GPS 将打开,反之亦然。

MainActivity.java:

package com.smscontrol;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText editText1, editText2;
Button buttonSaveMem1;

 @Override

/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

       editText1 = (EditText)findViewById(R.id.editText1);
       editText2 = (EditText)findViewById(R.id.EditText01);
       buttonSaveMem1 = (Button)findViewById(R.id.button1);

       buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);

       LoadPreferences();
 }

   Button.OnClickListener buttonSaveMem1OnClickListener = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   SavePreferences("loc_key", editText1.getText().toString());
   SavePreferences("gps_key", editText2.getText().toString());
   Toast.makeText(getBaseContext(), "Keywords Saved.", Toast.LENGTH_SHORT).show();
   LoadPreferences();
  }

   };
   private void SavePreferences(String key, String value){
       Context context = getApplicationContext();
          AppPrefs appPrefs = new AppPrefs(context);
          appPrefs.setLoc_key(editText1.getText().toString());
          appPrefs.setGPS_key(editText2.getText().toString());
       }

       private void LoadPreferences(){
        Context context = getApplicationContext();
        AppPrefs appPrefs = new AppPrefs(context);
        String strSavedMem1 = appPrefs.getLoc_key();
        String strSavedMem2 = appPrefs.getGPS_key();
        editText1.setText(strSavedMem1);
        editText2.setText(strSavedMem2);
       }
       public void turnGPSOnOff(){

            String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if(!provider.contains("gps"))
            { //if gps is disabled
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3")); 
                sendBroadcast(poke);
            } 
            else { //if gps is enabled
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3")); 
                sendBroadcast(poke);    
        }

       }       
}

AppPrefs.java:

package com.smscontrol;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class AppPrefs {

private static final String KEYWORDS = "KEYWORDS";
private SharedPreferences appSharedPrefs;
private SharedPreferences.Editor prefsEditor;
private String gps_key = "gps_key_prefs";
private String loc_key = "loc_key_prefs";

public AppPrefs(Context context){
    this.appSharedPrefs = context.getSharedPreferences(KEYWORDS, Activity.MODE_PRIVATE);
    this.prefsEditor = appSharedPrefs.edit();
    }

public String getGPS_key() {
    return appSharedPrefs.getString(gps_key, null);
    }

public void setGPS_key(String _gps_key) {
    prefsEditor.putString(gps_key, _gps_key).commit();
    }

public String getLoc_key() {
    return appSharedPrefs.getString(loc_key, null);
    }

public void setLoc_key(String _loc_key) {
    prefsEditor.putString(loc_key, _loc_key).commit();
    }

}

短信监听器.java:

package com.smscontrol;

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


public class SmsListener extends BroadcastReceiver {
    MainActivity Activity = new MainActivity();
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub



    AppPrefs appPrefs = new AppPrefs(context);
    String strSavedMem1 = appPrefs.getLoc_key();
    String strSavedMem2 = appPrefs.getGPS_key();

    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){

        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;

        String str = "";

        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                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 = msgs[i].getMessageBody().toString();

                    if (str.equals(strSavedMem1)){
                        Toast.makeText(context, "LOCATE", Toast.LENGTH_SHORT).show();
                    } else if (str.equals(strSavedMem2)){ 
                         Toast.makeText(context, "GPS", Toast.LENGTH_SHORT).show();
                    }
                }
            }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
            }

        }
    }
}

}

任何建议或帮助都可以。我论文的一部分急需。谢谢!

4

0 回答 0