0

我有一个问题,我无法从活动类获取数据到 PhoneStateListener 类。

为了更好地理解代码如下:

这是我要发送数据或字符串的类

package net.cellobject.blockingincomingcall;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SecondTab extends Activity
{
    EditText e1;
    Button b1;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.setting);

        e1=(EditText)findViewById(R.id.edt1);

        b1=(Button)findViewById(R.id.b1);

        LoadPreferences();

        b1.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                String msg=e1.getText().toString();

                SavePreferences("msg1",msg);

                LoadPreferences();

                if(msg=="")
                {
                    Toast.makeText(getApplicationContext(), "First 


                                               Enter the message then save it"

                                               ,Toast.LENGTH_LONG).show();

                    e1.requestFocus();
                }
            }
        });
    }

    private void LoadPreferences() 
    {
        SharedPreferences shp= getPreferences(MODE_PRIVATE);

        String s1=shp.getString("msg1","");

        e1.setText(s1);
    }

    private void SavePreferences(String key, String msg) 
    {
        SharedPreferences shp= getPreferences(MODE_PRIVATE);

        SharedPreferences.Editor editor=shp.edit();

        editor.putString(key, msg);

        editor.commit();
    }
}

这是我想要获取通过PhoneStateListener 扩展的数据或字符串的类?

package net.cellobject.blockingincomingcall;

import java.lang.reflect.Method;
import android.content.Context;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.telephony.ITelephony;


public class PhoneCallStateListener extends PhoneStateListener 
{   
    private Context context;    

    public PhoneCallStateListener(Context context)
    {
        this.context = context;
    }

    public void onCallStateChanged(int state, String incomingNumber) 
    {
        switch (state) 
        {
            case TelephonyManager.CALL_STATE_RINGING:               

                AudioManager audioManager = (AudioManager) context.

                                                 getSystemService(Context.AUDIO_SERVICE);   

                //Turn ON the mute

                audioManager.setStreamMute(AudioManager.STREAM_RING, true);     



                TelephonyManager telephonyManager = (TelephonyManager) context.

                                               getSystemService(Context.TELEPHONY_SERVICE);

                try {
                    @SuppressWarnings("rawtypes")

                    Class clazz = Class.forName(telephonyManager.getClass().getName());

                    Method method = clazz.getDeclaredMethod("getITelephony");

                    method.setAccessible(true);

                    ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);     

                    //Checking incoming call number

                    String incoming=incomingNumber.toString();

                    if (incomingNumber.equalsIgnoreCase(incoming)) 
                    {
                        Log.v("incoming_call",incoming);

                        telephonyService.endCall();

                        sendSMS(incoming, "I am Busy!!call me later");
                    }

                } catch (Exception e) {

                    e.printStackTrace();
                }

            audioManager.setStreamMute(AudioManager.STREAM_RING, false);

            break;
        }

        super.onCallStateChanged(state, incomingNumber);
    }

    private void sendSMS(String incoming, String string) 
    {
        android.telephony.SmsManager sms=android.telephony.SmsManager.getDefault();

        sms.sendTextMessage(incoming, null, string, null, null);
    }
}
4

2 回答 2

0

我发布了一个非常简单的方法来存储和获取首选项,使用它我将解决您与存储和检索首选项相关的问题。

public class Preferences {


public static void record(String key,Context context,String value)
{
    SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences .Editor editor=pref.edit();

    editor.putString(key,value );
    editor.commit();


}
public static String getRecord(String key,Context context)
{
    return PreferenceManager.getDefaultSharedPreferences(context).getString(key,PREF_DEFAULT_VALUE);

}

}

于 2012-04-13T13:21:29.973 回答
0

既然你Context在这两个地方都有,Activity and Receiver你可以SharedPreference用来设置和获取这两个地方的数据.. 什么问题..

context.getSharedPreference("name" , MODE_PRIVATE);
于 2012-04-13T13:09:50.353 回答