如何从活动类数据的PhoneStateListener类? [英] how to get data from activity class to the PhoneStateListener class?

查看:156
本文介绍了如何从活动类数据的PhoneStateListener类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在我不能获得活动类数据的PhoneStateListener类。

I have a problem in which i cant get data from activity class to the PhoneStateListener class.

为了更好地了解code是波纹管: -

for better understanding the code is as bellow:-

//这是从那里我要发送的数据或类字符串

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



    }

}

//这是我想要得到的数据或字符串,它是PhoneStateListener扩展的类?

//this is the class where i want to get the data or string which is extends with 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 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);



    }

}

请帮我出其真正迫切。

推荐答案

即时发布一个非常简单的方法来存储和获得preferences,使用此我将解决您的相关存储和检索preferences问题。

im posting a very simple method to store and get preferences, use this i will solve your problem related to store and retrieve preferences.

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);

}

}

这篇关于如何从活动类数据的PhoneStateListener类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆