无法将字符串从一个进程传递到另一个进程

时间:2012-04-13 12:32:24

标签: android

我无法从一个进程到另一个进程获取字符串。

我已经使用Intent解决了这个问题,但我的问题是我想要获取该字符串的类扩展了PhoneCallListener而不是Activity。

为了更好地理解我的问题,这里是代码:

// this is the class from where i want to send a string
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();
    }
}

// this is the class where i want to get the string
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 incomming=incomingNumber.toString();
                    if (incomingNumber.equalsIgnoreCase(incomming)) 
                    {
                        Log.v("incomming_call",incomming);
                        telephonyService.endCall();
                    sendSMS(incomming, "I am Busy!!call me latter");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


             audioManager.setStreamMute(AudioManager.STREAM_RING, false);
             break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
    private void sendSMS(String incomming, String string) 
    {
        android.telephony.SmsManager sms=android.telephony.SmsManager.getDefault();
        sms.sendTextMessage(incomming, null, string, null, null);
    }
}

1 个答案:

答案 0 :(得分:0)

how to get data from activity class to the PhoneStateListener class?

见上面的链接请不要一遍又一遍地重复同样的问题.. 感谢